1050. String Subtraction (20)

时间:2022-02-23 21:14:12

this problem  is from PAT, which website is http://pat.zju.edu.cn/contests/pat-a-practise/1050.

firstly i think i can use double circulation to solve it ,however the result of two examples is

proofed to be running time out. So as the problem said, it is not that simple to make it fast.

then i search the problem , there are many people using the search table. i think it is a very

good idea. so i changed the algorithm.

#include<iostream>
#include<string>
#include<vector>
using namespace std; int MAX=; int main()
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
vector<bool> isExisted(MAX,false);
for (int i = ; i < s2.size(); i++)
{
isExisted[s2[i]] = true;
}
for (int i = ; i < s1.size(); i++)
{
if (!isExisted[s1[i]]) cout<<s1[i];
}
cout<<endl;
}

so this time the Time complexity is O(n) ,which is much faster than the previous one.