0%

PAT A1084 Broken Keyboard

题目链接

解题思路

利用ASCII码有255位当做散列的index,然后利用第一个字符串遍历,这个遍历的顺序再去索引ans这个数组。这个数组全部初始化为0,遍历第一个字符串每次都加一,第二个字符串每次都减一,最后大于0的就按照第一个字符串的顺序遍历一遍,如果输出过了就设为-1.

注意

要严格按照题目条件,一个不注意就掉坑里,题目中说字符串or’_’也就是空格也可能坏了!

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdio>
#include <cstring>
#include <cctype>

char rstr[90];
char ostr[90];
int ans[300];

int main() {
scanf("%s",rstr);
scanf("%s",ostr);
memset(ans, 0, sizeof(ans));
int l1 = strlen(rstr);
int l2 = strlen(ostr);
for (int i = 0; i < l1; i++) {
if(isalpha(rstr[i])) ans[toupper(rstr[i])]++;//转为大写保存
else ans[rstr[i]]++;
}
for (int i = 0; i < l2; i++) {
if (isalpha(ostr[i])) ans[toupper(ostr[i])]--;
else ans[ostr[i]]--;
}
for (int i = 0; i < l1; i++) {
if (isalpha(rstr[i])){
if (ans[toupper(rstr[i])] > 0 && ans[toupper(rstr[i])] != -1) {
printf("%c", toupper(rstr[i]));
ans[toupper(rstr[i])] = -1;//只打印一次同样的字母
}
}else{
if (ans[rstr[i]] > 0 && ans[rstr[i]] != -1) {
printf("%c", rstr[i]);
ans[rstr[i]] = -1;
}
}
}

return 0;
}

本文标题:PAT A1084 Broken Keyboard

文章作者:GavinYGM

发布时间:2020年08月26日 - 23:08

最后更新:2020年08月27日 - 00:08

原始链接:http://www.gavinygm.cn/2020/08/26/PAT-A1084-Broken-Keyboard/

许可协议: 转载请保留原文链接及作者。