PAT甲级 A1050
题目详情
1050 String Subtraction (20分)
Given two strings S1 and S2, S=S1−S2is 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 10e4. 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−S2in one line.
Sample Input:
They are students.
aeiou
Sample Output:
Thy r stdnts.
解题思路
这道题可以用map结合哈希表的形式进行快速解题。map可以将char字符转化成符号,然后用哈希表表示该char字符是否在S2中,遍历S1中的字符,若存在S2中的字符,则不输出。需要注意的是,读入S2的内容也要用getline(cin,s)的形式,要不测试点3过不去
以下为AC代码
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<memory.h>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
using namespace std;
map<char, int> ers;
int ctoint(char a) {
if (ers.find(a) == ers.cend()) {
return 0;
}
else {
return 1;
}
}
int main() {
string N;
getline(cin, N);
string t;
getline(cin,t);
for (int i = 0; i < t.length(); i++) {
ers[t[i]] = 1;
}
for (int i = 0; i < N.size(); i++) {
if (ctoint(N[i]) == 0) {
cout << N[i];
}
else {
continue;
}
}
cout << '\n';
}