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

题目描述:在一个坏掉的键盘上,有些键是损坏的,我们通过输入一串字符串,来找到那些坏掉的键。


输入:第一行一串字符串,若键盘没有坏,他应该像第一行的字符串这样完整的显示在电脑屏幕上。第二行也是字符串,他是坏掉的键盘输入后,显示在电脑上的效果。

输出:坏掉的键值,若是拼音,则输出他的大写形式。


解题思路:两个字符串同时从索引0开始扫描,对比一下两个字符串,结果就出来了。哦,因为题目要求输出字母大写的形式,若字符串中出现小写字母,要转换处理一下再输出。


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s1, s2;
	getline(cin,s1);
	getline(cin,s2);
	int c = 0;
	map<char,bool> p;
	for(int i=0; i<s1.length(); i++){
		char ch1=s1[i], ch2=s2[c];
		if(s1[i]>='a'&&s1[i]<='z') ch1 = s1[i]-'a'+'A';
		if(s2[c]>='a'&&s2[c]<='z') ch2 = s2[c]-'a'+'A'; 
		if(ch1!=ch2&&p[ch1]==false){
			cout << ch1;
			p[ch1] = true;
		}
		else if(ch1==ch2) c++;
	}
    cout << endl;
	return 0;
}