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
题意
给出一个输入字符串和一个输出字符串,按顺序求坏键。其中字母的坏键统一用大写形式输出
思路
使用两个长度为256的布尔数组分别标记字符是否被输入和是否输出(一个坏键只输出一次)。先遍历显示字符串得到被输入的字符,在遍历输入字符串打印坏键。为了处理字母的极端问题(比如同时没有显示字符“a”和"A",此时坏键只有一个“A”),将Ascii码统一转换成大写字母(如果是一个小写字母的话)。
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
#define L 256
// 将字符统一处理成大写字母,避免坑(比如同时没有输出"a"和"A"时,坏键只有"A")
int ascii(char c){
return islower(c) ? toupper(c) : c;
}
int main() {
bool showed[L], printed[L];
fill(showed, showed + L, false);
fill(showed, printed + L, false);
char origin[81], typed[81];
scanf("%s%s", origin, typed);
// 遍历显示字符串, 标记已显示的字符
for(int i = 0, l = (int)strlen(typed); i < l; i++){
showed[ascii(typed[i])] = true;
}
// 遍历输入字符串
for(int i = 0, l = (int)strlen(origin), t; i < l; i++){
t = ascii(origin[i]);
// 如果该字符没有被显示并且没有被输出过,则找到一个坏键
if(!showed[t] && !printed[t]){
putchar(t);
printed[t] = true;
}
}
return 0;
}