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
注意点:
- 采用散列的思想,将每一个字符映射为一个数字(ASCII),再将该数字作为Hash表的下标
- 俩个字符串,应将不完整的字符串作为映射的目标,而完整的字符串作为查询
- Hash表里的元素可能有三个值:
-1:该字符没有打印出来,且是第一次输出坏字符对应的按键;
0 :该字符没有打印出来,但之前输出过坏字符对应的按键了;
1:该字符打印出来了,说明对应的键盘按键是好的;
只需要打印元素值为-1所代表的字符(大写) - 注意大写字母和小写字母对应的映射数字是相同的,ASCII码相差32;
5. 注意在初始化Hash表的元素值为-1时,只能在main函数里用memset,不能在main函数外初始化!
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int hashTable[1000];
void hashFunc(char str[],int n)
{
int id;
for(int i=0;i<n;i++)
{
if(str[i]>='a'&&str[i]<='z') id=(int)str[i]-32;
else id=(int)str[i];
hashTable[id]=1;
}
}
int main(void)
{
memset(&hashTable[0],-1,sizeof(hashTable));
char instr[100],outstr[100];
scanf("%s %s",instr,outstr);
hashFunc(outstr,strlen(outstr));
for(int i=0;i<strlen(instr);i++)
{
int index;
if(instr[i]>='a'&&instr[i]<='z') index=(int)instr[i]-32;
else index=(int)instr[i];
if(hashTable[index]<0)
{
printf("%c",index);
hashTable[index]=0;
}
}
}