1029 旧键盘 (20 分)

题目:1029 旧键盘 (20 分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。

输入格式:

输入在 2 行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过 80 个字符的串,由字母 A-Z(包括大、小写)、数字 0-9、以及下划线 _(代表空格)组成。题目保证 2 个字符串均非空。

输出格式:

按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有 1 个坏键。

输入样例:

7_This_is_a_test
_hs_s_a_es

输出样例:

7TI

思路:

  • 其实思路是很简单的,用c++中的STL容器代码就特别简便。
  1. 用string函数定义三个变量名后,用string中的find()函数在s2中寻找是否有s1中的字符,如果有并且ans中不存在该字符,也就是说该字符还没有找过就将符合的字符加到ans中去。
  2. 用string::npos来判断是否找到,string::npos 是一个常数,其本身的值等于-1,但是由于是unsigned int类型,因此也可以认为是unsigned int类型的最大值(4294967295)。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <sstream>
 5 #include <cmath>
 6 #include <algorithm>
 7 #include <string>
 8 #include <stack>
 9 #include <queue>
10 #include <vector>
11 #include <map>
12 using namespace std;
13 
14 int main()
15 {
16     string s1, s2, ans;
17     cin >> s1 >> s2;
18     for(int i = 0; i < s1.length(); i++)
19     {
20         if(s2.find(s1[i]) == string::npos && ans.find(toupper(s1[i])) == string::npos)
21             ans += toupper(s1[i]);
22     }
23     cout << ans;
24     return 0;
25  } 

总结:

猜你喜欢

转载自www.cnblogs.com/Anber82/p/11271621.html
今日推荐