AcWing 1534. 字符串减法
原题链接
简单
作者:
Wegoon
,
2021-09-06 16:02:56
,
所有人可见
,
阅读 287
知识点:字符串处理、哈希表
我的做法
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
unordered_map<char, bool> m;
string s, s1, s2;
int main() {
getline(cin, s1), getline(cin, s2);
for (int i = 0; s2[i]; i ++ ) m[s2[i]] = true;
for (int i = 0; s1[i]; i ++ ) if(!m[s1[i]]) s += s1[i];
cout << s << endl;
return 0;
}
看了y总后的改进做法
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>
using namespace std;
unordered_set<char> m;
string s, s1, s2;
int main() {
getline(cin, s1), getline(cin, s2);
for (int i = 0; s2[i]; i ++ ) m.insert(s2[i]);
for (int i = 0; s1[i]; i ++ ) if(!m.count(s1[i])) s += s1[i];
cout << s << endl;
return 0;
}