PAT-1084 Broken Keyboard

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/eric4784510/article/details/82108513

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

    简单题

#include<stdio.h>
#include<string.h>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
char toUper(char a){
	if(a>='a'&&a<='z')
		return a+'A'-'a';
	return a;
}
bool issame(char a,char b){
	if(a==b)
		return true;
	if(a>='a'&&a<='z'&&b==a+'A'-'a')
		return true;
	if(b>='a'&&b<='z'&&a==b+'A'-'a')
		return true;

	return false;
}
int main(){
	char a[100],b[100];
	scanf("%s%s",a,b);
	vector<int> ans;
	int i=0,j=0;
	for(;i<strlen(a)&&j<strlen(b);){
		if(issame(a[i],b[j])){
			i++;j++;
		}else{
			for(;a[i]!=b[j]&&i<strlen(a);i++){
				ans.push_back(toUper(a[i]));
			}
		}
	}
	for(;i<strlen(a);i++){
		ans.push_back(toUper(a[i]));
	}
	set<int> t;
	for(int i=0;i<ans.size();i++){
		if(t.find(ans[i])==t.end())
			t.insert(ans[i]);
		else{
			ans.erase(ans.begin()+i);
			i--;
		}
	}
	for(int i=0;i<ans.size();i++){
		printf("%c",ans[i]);
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/eric4784510/article/details/82108513