原题:https://pintia.cn/problem-sets/994805342720868352/problems/994805382902300672
这道题让你找损坏的键盘字符
思路很简单,第一:对所有字符开散列;第二:把小写字母转变成大写;第三:一个个匹配,但是要注意当一个字符串匹配到最后时的特殊处理。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include<stack>
#include<queue>
using namespace std;
const int MaxN = 1000;
int str_w[MaxN]; //看看这个坏字符是不是已经被记下
void to_upper(string &s) {
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] >= 'a' && s[i] <= 'z')
s[i] = s[i] - 32;
}
}
//'a'=='A'+32
int main() {
fill(str_w, str_w + MaxN, -1);
string s, s_worn; //original string, typed-out string
cin >> s >> s_worn;
//全部变成大写字母
to_upper(s);
to_upper(s_worn);
int i = 0, j = 0;
vector<char> alpha; //这个是坏掉的字符
while (i < s.size() || j < s_worn.size()) {
if (j == s_worn.size()) { //特殊情况
if (str_w[s[i]] == -1) { //字符坏掉且未被记录
str_w[s[i]] = 1;
alpha.push_back(s[i]);
}
i++;
continue;
}
if (s[i] == s_worn[j] ) {
i++; j++;
continue;
}
if (str_w[s[i]] == -1) { //字符坏掉且未被记录
str_w[s[i]] = 1;
alpha.push_back(s[i]);
}
i++;
}
for (i = 0; i < alpha.size(); i++) {
if (alpha[i] >= 'a' && alpha[i] <= 'z')
cout << char(alpha[i] - 32);
else {
cout << alpha[i];
}
}
cout << endl;
return 0;
}