1029. 旧键盘(20)
原题链接
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。
输入格式:
输入在2行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过80个字符的串,由字母A-Z(包括大、小写)、数字0-9、以及下划线“_”(代表空格)组成。题目保证2个字符串均非空。
输出格式:
按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有1个坏键。
输入样例:
7_This_is_a_test
_hs_s_a_es
输出样例:
7TI
注意:
- 输出格式按照发现顺序输出,这个很重要
- 最后通过的代码思路,先将两个字符串中小写字母转化为大写
- 遍历实际输出的字符串b,只要输入的字符串a中与b重复的字母,全部替换成‘#’
- 遍历a,将其中没被替换的字符添加到vector数组中,注意不要添加重复字符
代码:
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;
int main()
{
string a,b;
cin >> a >> b;
int alen = a.size();
int blen = b.size();
for(int i=0; i<alen; i++)
a[i] = toupper(a[i]);
for(int i=0; i<blen; i++)
b[i] = toupper(b[i]);
for(int i=0; i<blen; i++){
for(int j=0; j<alen; j++){
if(b[i] == a[j])
a[j] = '#';
}
}
vector<char> res;
for(int k=0; k<alen; k++){
if(a[k] != '#'){
bool temp = false;
for(int n=0; n<res.size(); n++){
if(res[n] == a[k]){
temp = true;
break;
}
}
if(!temp)
res.push_back(a[k]);
}
}
for(int n=0; n<res.size(); n++)
cout << res[n];
return 0;
}
- 后来看了别人的代码,觉得这个也不错,点击链接
- 最后的输出,题目要求按照发现顺序输出,第一次写代码没注意,虽然输出了,但是顺序不对,也贴出来吧
错误代码:
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
int main()
{
string a,b;
cin >> a >> b;
int alen = a.size();
int blen = b.size();
for(int i=0; i<alen; i++)
a[i] = toupper(a[i]);
for(int i=0; i<blen; i++)
b[i] = toupper(b[i]);
int HASH[37] = {0};
for(int i=0; i<alen; i++){
if(a[i]>='0'&&a[i]<='9'){
HASH[a[i]-'0'] = 1;
continue;
}
if(a[i]>='A'&&a[i]<='Z'){
HASH[a[i]-'A'+10] = 1;
continue;
}
if(a[i]=='_')
HASH[36] = 1;
}
for(int i=0; i<blen; i++){
if(b[i]>='0'&&b[i]<='9'){
HASH[b[i]-'0'] = 0;
continue;
}
if(b[i]>='A'&&b[i]<='Z'){
HASH[b[i]-'A'+10] = 0;
continue;
}
if(b[i]=='_')
HASH[36] = 0;
}
for(int i=0; i<37; i++){
if(HASH[i] != 0){
if(i <= 9){
char temp = '0'+i;
cout << temp;
}
else if(i <= 35){
char temp = 'A'+(i-10);
cout << temp;
}
else
cout << '_';
}
}
return 0;
}