Cod sursa(job #2664909)

Utilizator MattiaMattia Iojica Mattia Data 29 octombrie 2020 18:28:54
Problema Trie Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <bits/stdc++.h>

using namespace std;

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

map <string, int> v;

int x;
string s;

int main()
{
    while(f >> x >> s)
    {
        if(x == 0)
        {
            v[s] += 1;
        }
    else
        if(x == 1)
        {
            v[s] -= 1;
        }
    else
        if(x == 2)
        {
            cout << v[s] << '\n';
        }
    else
        if(x == 3)
        {
            int maxi = 0;
            map <string, int>::iterator it = v.begin();
            while(it != v.end())
            {
                string q = it -> first;
              //  cout << q << '\n';
              if(it->second != 0)
              {
                if(s[0] != q[0])
                {
                    it++;
                    continue;
                }
                else
                {
                   // cout << '\n';
                   // cout << q[0] << " " << s[0] <<'\n';
                    int loc = 1;
                    while(q[loc] == s[loc] && loc < min(q.size(), s.size()))
                    {
                     //   cout << q[loc] << " " << s[loc] <<'\n';
                        loc++;
                    }

                    maxi = max(loc, maxi);
                }
              }
                it++;
            }
                cout << maxi << '\n';
        }
    }


    return 0;
}