1084 Broken Keyboard (20 point(s))
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
题目大意
现有一块某些键已经坏掉的破键盘。所以当你键入一些句子的时候,某些字符可能打不出来。现在给出一串你先输入的字符串以及你最终键入的字符串,请找出那些坏掉的键
解题思路
首先建立两个hash数组,hashTable1表示坏键,hashTable2表示没有坏的键。读入str,之后依次读入最终输出的字符,在hashTable2相应位置置1,再遍历str,如果该字符对应位置hashTable2中为0,则说明该键坏了,在hashTable1中相应位置置1。之后遍历str,如果字符在hashTable1相应位置的值为1,则输出,并将该位置置0。注意:若为字母,则输出大写
代码
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
char str[85];
int hashTable1[128] = { 0 }, hashTable2[128] = { 0 };//hashTable1表示坏键,hashTable2表示没有坏的键
int ret = scanf("%s", str);
ret = getchar();
char c;
while ((c = getchar()) != '\n') {//最终打出来的字符
if (islower(c)) c -= 32;//小写-->大写
hashTable2[c - '0'] = 1;
}
for (int i = 0; i < strlen(str); ++i) {
if (islower(str[i])) str[i] -= 32;
if (hashTable2[str[i] - '0'] == 0) {//坏键,在hashTable1相应位置置1
hashTable1[str[i] - '0'] = 1;
}
}
for (int i = 0; i < strlen(str); ++i) {
if (hashTable1[str[i] - '0'] == 1) {//坏键输出一次后将相应位置置0
printf("%c", str[i]);
hashTable1[str[i] - '0'] = 0;
}
}
return 0;
}