Cod sursa(job #2908769)

Utilizator mihneazzzMacovei Daniel mihneazzz Data 5 iunie 2022 18:59:44
Problema Trie Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
int op;string s;
unordered_map<string,int>M,M1;
void Adaugare()
{
    string prefix;
    for(auto x:s) prefix.push_back(x),M1[prefix]++;
}
void Stergere()
{
    string prefix;
    for(auto x:s) prefix.push_back(x),M1[prefix]--;
}
int main()
{
    while(fin>>op>>s)
        if(!op) M[s]++,Adaugare();
        else if(op==1) M[s]--,Stergere();
        else if(op==2) fout<<M[s]<<"\n";
        else
        {
            int lgmax=0;
            string prefix;
            for(auto x:s)
            { prefix.push_back(x);
              if(M1[prefix]) lgmax=prefix.size();
            }
            fout<<lgmax<<"\n";cout<<"\n";
        }
    return 0;
}