[PAT A1050]String Subtraction
题目描述
1050 String Subtraction (20 分)Given two strings S?1?? and S?2??, S=S?1???S?2?? is defined to be the remaining string after taking all the characters in S?2?? from S?1??. Your task is simply to calculate S?1???S?2?? for any given strings. However, it might not be that simple to do it fast.
输入格式
Each input file contains one test case. Each case consists of two lines which gives S?1?? and S?2??, respectively. The string lengths of both strings are no more than 10?4??. 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.
输出格式
For each test case, print S?1???S?2?? in one line.
输入样例
They are students.
aeiou
输出样例
Thy r stdnts.
解析
- 简单题,题目大意就是输入两个个字符串s1,s2,然后对于所有在s2中出现的字符,在s1中将其删去即可
- 需要注意的地方就是我的代码中是使用了字符串ans来存储结果,最后将结果输出,其实在遍历的过程中输出就可以了,不需要新建一个string
#include<iostream>
#include<string>
using namespace std;
bool hashTable[256];
int main()
{
string str1, str2, ans;getline(cin, str1);getline(cin, str2);for (int i = 0; i < str2.length(); i++)hashTable[str2[i]] = true;for (int i = 0; i < str1.length(); i++) {
if (!hashTable[str1[i]]) ans += str1[i];}printf("%s", ans.c_str());return 0;
}