题目连接:https://www.patest.cn/contests/pat-a-practise/1050
题目大意:给定两个字符串S1,S2,从S1中删除S2中出现的所有字符,并输出剩余字符。
小tips:
1.注意字符串含空格
2.双层for循环的方法会超时
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int main(){
string a,b;
getline(cin,a);
getline(cin,b);
bool flag[150]={false};
for(int i=0;i<b.length();i++)
flag[b[i]]=true;
for(int i=0;i<a.length();i++){
if(flag[a[i]]==false)
cout<<a[i];
}
return 0;
}
采用的方法是定义一个bool型数组,用来标记哪个字符在字符串2中出现了,数组的下标是字符的ASCII码。用字符作为数组下标即是将该字符的ASCII码值作为下标,详情请见