PAT 1050.String Subtraction

题目

Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 - S2 for any given strings. However, it might not be that simple to do it fast.

Input Specification:

Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.

Output Specification:

For each test case, print S1 - S2 in one line.

Sample Input:

They are students.
aeiou

Sample Output:

Thy r stdnts.

我的解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <set>
#include <cctype>

//解题思路:
//1.用string保存s1,set保存s2
//2.遍历s1,若s2中有,则不打印

using namespace std;

int main()
{
string s1;
set<char> s2;
char c;

getline(cin, s1);
while ((c = cin.get()) != '\n') {
s2.insert(c);
}

for (auto x : s1) {
if (s2.find(x) == s2.end()) {
cout << x;
}
}
cout << endl;

return 0;
}
Author: sphc
Link: https://jkuvw.xyz/archives/8c5826eb/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
微信打赏
支付宝打赏