1050. String Subtraction (20)

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=1000;



int main()

{

    string s1,s2;

    getline(cin,s1);

    getline(cin,s2);

    vector<bool> isExisted(MAX,false);

    for (int i = 0; i < s2.size(); i++)

    {

        isExisted[s2[i]] = true;

    }

    for (int i = 0; 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.

你可能感兴趣的:(String)