PAT1084 Broken Keyboard (20 分)

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




解析

首先:大小写是不敏感的。所有把输入的两个字符串都改成全部大写。
使用set标记在第二个字符串出现的字符。
在遍历第一个字符串:看看那些字符是前面set里没有的。就是要旧键盘打不出的。我又用一个set是为个去重。

#include<iostream>
#include<string>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
	string orginal, tape_out;
	cin >> orginal >> tape_out;
	transform(orginal.begin(), orginal.end(), orginal.begin(), ::toupper);
	transform(tape_out.begin(), tape_out.end(), tape_out.begin(), ::toupper);
	string broke;
	set<char> Set(tape_out.cbegin(), tape_out.cend());
	set<char> show;
	for (auto x : orginal) {
		if (Set.find(x) == Set.end() && show.find(x) == show.end()) {
			broke += x;
			show.insert(x);
		}
	}
	cout << broke;
}

猜你喜欢

转载自blog.csdn.net/weixin_41256413/article/details/84882105