Cod sursa(job #3166388)

Utilizator MateiCatalinUrsache Matei MateiCatalin Data 8 noiembrie 2023 17:59:37
Problema Trie Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
#include <map>
#include <string>

using namespace std;


ifstream f("trie.in");
ofstream g("trie.out");

int w;
string cuv;
map <string,int> mp,pref;

int main()
{
    while(f>>w)
    {
        f>>cuv;
        if(w==0)
        {
            mp[cuv]++;
            while(cuv!="")
            {
                pref[cuv]++;
                cuv.pop_back();
            }
        }
        if(w==1)
        {
            mp[cuv]--;
            while(cuv!="")
            {
                pref[cuv]--;
                cuv.pop_back();
            }
        }
        if(w==2)
            g<<mp[cuv]<<'\n';
        if(w==3)
        {
            int steag=0;
            while(cuv!="" && steag==0)
            {
                if(pref[cuv]>0)
                {
                    g<<cuv.size()<<'\n';
                    steag=1;
                }
                cuv.pop_back();
            }
            if(steag==0)
                g<<0<<'\n';
        }
    }
    return 0;
}