1084 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

解题思路:

这道题目使用c++的STL库的话会非常简单:

1.首先用两个string类型变量a和b,存储应该输入文字以及实际输入文字。 

2.枚举第一个字符串中的字符,对当前枚举字符c1,枚举第二个字符串中的字符(设为c2),如果c1和c2像等则说明c1字符按键没坏,然后继续枚举下一个c1和c2;否则,说明当前c1按键已坏,将当前已坏的字符加入res字符串中(如果已存在不加入,否则对于字母转换为大写后加入),然后枚举下一个c1。

#include<iostream>
#include<ctype.h>
#include<string>
using namespace std;

int main()
{
	string res = "";
	string a, b;
	cin >> a >> b;
	int pos = 0;
	for (int i = 0; i < a.length(); i++)
	{ 
		if (a[i] == b[pos])    //没坏
			pos++; 
		else                         //否则坏了
		{ 
			if (!isalpha(a[i]) && res.find(a[i]) == res.npos)   //判断res中是否存在该字符
				res += a[i];
			else
			{
				if (res.find(toupper(a[i])) == res.npos)
					res += toupper(a[i]);
			}
		}
	}
	cout << res << endl;
	return 0;
}
发布了119 篇原创文章 · 获赞 22 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/lovecyr/article/details/104749593