题目
给定两个字符串 S1 和 S2,S=S1−S2 定义为将 S1 中包含的所有在 S2 中出现过的字符删除后得到的字符串。
你的任务就是计算 S1−S2。
输入格式
共两行,第一行包含字符串 S1,第二行包含字符串 S2。
输出格式
输出共一行,表示 S1−S2 的结果。
数据范围
两个给定字符串的长度都不超过 104。
输入样例:
They are students.
aeiou
输出样例:
Thy r stdnts.

解法:模拟

时间复杂度O(n),空间复杂度O(n)

  1. #include <iostream>
  2. #include <unordered_map>
  3. using namespace std;
  4. int main() {
  5. string a, b;
  6. getline(cin, a), getline(cin, b);
  7. unordered_map<char, int> h;
  8. for (auto c: b) {
  9. h[c] = 1;
  10. }
  11. string ans;
  12. for (auto c: a) {
  13. // cout << c << ' ' << h[c] << endl;
  14. if (h[c] == 0)
  15. ans += c;
  16. }
  17. cout << ans;
  18. return 0;
  19. }