题目:旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。
思路:遍历输入的字符串,通过find()算法确定在实际的输出中是否有对应的字符,若没有则记录
代码:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string>s1(2);
string temp;
for (auto it = s1.begin(); it != s1.end(); ++it)
{
cin >> temp;
*(it) = temp;//输入,*(it)是vector迭代器的解引用
}
if (s1[0].empty() || s1[1].empty()||s1[0].length()>80||s1[1].length()>80||s1[0].length()<s1[1].length())
{
return 0;
}
int size = s1[0].length();
for (auto it = s1.begin(); it != s1.end(); it++)
{//对字符串进行大写转换
for (auto mi = (*it).begin(); mi != (*it).end(); mi++)
{
(*mi)=toupper((*mi));
}
}
string index(size, ‘0‘);//保存结果
int step = 0;
for (auto it = s1[0].begin(); it != s1[0].end(); ++it)
{
char ch = *it;
if (index.find(ch)==string::npos)//在index数组中寻找是否记录过该字符
{
if (s1[1].find(ch) == string::npos)//在实际的输出中寻找是否有字符ch
{//判断条件表示没有字符ch时,保存ch至index中
index[step] = ch;
step++;
}
}
}
for (int i = 0; i <step; i++)
{
cout << index[i];
}
return 0;
}
原文:https://www.cnblogs.com/zongji/p/12403803.html