题目描述
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出
肯定坏掉的那些键。
输入描述:
输入在2行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过80个字符的串,由字母A-Z(包括大、小写)、数字0-9、
以及下划线“_”(代表空格)组成。题目保证2个字符串均非空。
输出描述:
按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有1个坏键。
输入例子:
7_This_is_a_test
_hs_s_a_es
输出例子:
7TI
一个好用的库可以使代码变得简单——#include<cctype>
具体用法可以参考http://baike.baidu.com/view/2058283.htm
find函数用于寻找某个序列的在string中第一次出现的位置。
string::npos参数 —— npos 是一个常数,用来表示不存在的位置
//如果字符串不存在包含关系,那么返回值就一定是npos
if(a.find(b)!=string::npos){
cout<<"yes"<<endl;
}else{
cout<<"no"<<endl;
}
简洁代码:
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
string s1,s2,ans;
cin>>s1>>s2;
for(int i=0;i<s1.length();i++){
if(s2.find(s1[i])==string::npos&&ans.find(toupper(s1[i]))==string::npos)
ans+=toupper(s1[i]);//topper是函数库中的函数,不是类中的函数。这里不能写成
//s1[i].topper!
}
cout<<ans;
return 0;
}
另一种方法(散列)
#include <iostream>
#include<cstring>
using namespace std;
int main()
{
char str1[100], str2[100];
bool flag[128] = { false };
gets(str1);
gets(str2);
int m = strlen(str1), n = strlen(str2);
for (int i = 0; i < m; i++)
{
char c1, c2;
c1 = str1[i];
if (c1 >= 'a'&&c1 <= 'z') c1 -= 32;
int j;
for ( j = 0; j < n; j++)
{
c2 = str2[j];
if (c2 >= 'a'&&c2 <= 'z') c2 -= 32;
if (c1 == c2) break;
}
if (j == n && flag[c1] == false)
{
printf("%c", c1);
flag[c1] = true;
}
}
return 0;
}