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

题⽬⼤意:

旧键盘上坏了⼏个键,于是在敲⼀段⽂字的时候,对应的字符就不会出现。现在给出应该输⼊的⼀段⽂字、以及实际被输⼊的⽂字,请你列出肯定坏掉的那些键
题⽬⼤意:旧键盘上坏了⼏个键,于是在敲⼀段⽂字的时候,对应的字符就不会出现。现在给出应该输⼊的⼀段⽂字、以及实际被输⼊的⽂字,请你列出肯定坏掉的那些键。

分析:

首先将需要输入的字符串s1的所有字符转成大写,并记录所有字符键为损坏broken[s1[i]]=true,遍历实际输出的字符串,将这些字符的改未损坏状态broken[s2[i]]=false,最后遍历需要输出的字符串,判断是否损坏。

//1084 Broken Keyboard (20分)
#include <iostream>
#include <string>
using namespace std;
bool broken[256];
bool vis[256];
int main()
{
	string s1,s2;
	getline(cin,s1);
	getline(cin,s2);
	for(int i=0; i<s1.size(); i++)
	{
		if(isalpha(s1[i]))
			s1[i]=toupper(s1[i]);
		broken[s1[i]]=true;
	}
	for(int i=0; i<s2.size(); i++)
	{
		if(isalpha(s2[i]))
			s2[i]=toupper(s2[i]);
		broken[s2[i]]=false;
	}
	for(int i=0; i<s1.size(); i++)
	{
		if(broken[s1[i]]==true)
		{
			vis[s1[i]]=true;
			cout<<s1[i];
		}
	}
	return 0;
}
发布了21 篇原创文章 · 获赞 1 · 访问量 554

猜你喜欢

转载自blog.csdn.net/DayDream_x/article/details/104362498