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
题意为给定两个字符串s1,s2。将在将s1中出现而未在s2中出现的字符输出。用一个哈希hash存储在s2中出现的字符的ASCLL码,并令hash[s2[i]][0]++;然后遍历s1,将满足hash[s1[i]][0]==0&&hash[s1[i]][1]==0的字符输出,其中hash[s1[i]][1]==0控制只输出一次。注意要将小写字母转换为大写字母输出!
参考代码如下:
#include <cstdio>
#include <cstring>
int hash[128][2]; //hash[i][0]、hash[i][1]分别记录有无对应字符及是否是首次输出
char org[100];
char out[100];
int main()
{
while(scanf("%s",org)!=EOF) //因为空格用'_'代替,故可以用scanf
{
scanf("%s",out);
memset(hash,0,sizeof(hash));
int len1=strlen(org);
int len2=strlen(out);
for(int i=0;i<len2;++i) //先寻找可以正常输出的
{
if(out[i]>='a'&&out[i]<='z') //转换为大写字母
out[i]=out[i]-'a'+'A';
hash[out[i]][0]++; //对应数字数+1
}
for(int i=0;i<len1;++i)
{
if(org[i]>='a'&&org[i]<='z')
org[i]=org[i]-'a'+'A';
if(hash[org[i]][0]==0&&hash[org[i]][1]==0) //第二次输入中未出现且未输出
{
printf("%c",org[i]);
hash[org[i]][1]++;
}
}
printf("\n");
}
return 0;
}