1084 Broken Keyboard (20分)
-------题目链接-------
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.
Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.
Input Specification:
Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or _ (representing the space). It is guaranteed that both strings are non-empty.
Output Specification:
For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.
Sample Input:
7_This_is_a_test
_hs_s_a_es
Sample Output:
7TI
思路总结:
1.思路: 对于输入的两串字符串分别存储,用短字符串做hash
,再遍历长字符串对照hash
表,把不在hash表的字符输出。
2.易错点:
① 在遍历长字符串与hash
表对比时,要分当前字符是大写还是小写。若当前字符在hash
表中为false
(则该字符为预输出字符)。 且该字符若是大写字母, 则对应的小写字符也必定不在hash表中,则另这一对字符的hash
表对应值变为true
以避免重复输出; 字符为小写时同理;
②最后输出时要注意小写字母要转换成大写字母,可在遍历时判断出为小写字母时即将其转换为大写。
AC代码
#include<cstdio>
#include<cstring>
bool hash[200] = {false}; //hash表
int main()
{
char all[82]; //长字符串
char str[82]; //短字符串
scanf("%s%s",all,str);
for(int i=0; i<strlen(str); i++){ //遍历短字符串
hash[str[i]-'\0'] = true; //做字符与其对应ASCII码值的hash
}
for(int i=0; i<strlen(all); i++){ //遍历长字符串
char c = all[i];
int key = c - '\0';
if(hash[key] == false){ //表示该字符为预输出字符,接下来处理该字符与hash表
if(c >= 'a' && c <= 'z'){ //若为小写
hash[c - 'a' + 'A' - '\0'] = true; //对应大写字母hash表置为true
c = c - 'a' + 'A'; //转换为大写以供输出
}
else if(c >= 'A' && c <= 'Z'){ //若为大写字母,同理
hash[c - 'A' + 'a' - '\0'] = true;
}
hash[key] = true; //字符对应的hash值置为true
printf("%c",c); //输出该字符
}
}
return 0;
}