1050 String Subtraction(20 分)
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.
入坑无法自拔,需要输入两段带空格的语句,想起来的有scanf("%[^\n]",str);不过这种在输入第二段字符串的时候需要加一句getchar();读取第一行结尾的\n(在缓冲区中)当时忘记加了,于是造成第二段无法读取,然后又想起c++中getline,这个就比较简单了,直接读取一行中的数据。
#include<iostream>
using namespace std;
int main()
{
string str1,str2;
getline(cin,str1);
getline(cin,str2);
bool f[150] = {false};
for(int i = 0;i < str2.length();i++){
f[str2[i]] = true;
}
for(int i = 0;i < str1.length();i++){
if(f[str1[i]] == false){
cout << str1[i];
}
}
cout << endl;
return 0;
}