Problem Description:
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
Problem Analysis:
由于题目中所给的字符串只会出现 A
- Z
,a
- z
,0
- 9
和 -
,因此我们可以用 ASCII 值来将其映射至 int
整数,并且这样不会造成冲突。
注意,这里的大写字母和小写字母由于是一个键,因此二者要映射成同一个值。
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100;
bool has[N];
bool st[N];
int main()
{
string origin, word;
cin >> origin >> word;
for (int i = 0; i < word.size(); i ++ )
{
int ascii = word[i];
has[ascii] = true;
if (ascii >= 65 && ascii <= 90)
has[ascii + 32] = true;
if (ascii <= 122 && ascii >= 97)
has[ascii - 32] = true;
}
for (int i = 0; i < origin.size(); i ++ )
{
int ascii = origin[i];
if (!has[ascii])
{
if (ascii <= 122 && ascii >= 97)
{
if (!st[ascii - 32])
{
printf("%c", ascii - 32);
st[ascii - 32] = true;
}
}
else
{
if (!st[ascii])
{
printf("%c", origin[i]);
st[ascii] = true;
}
}
}
}
return 0;
}