Cod sursa(job #2311981)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 3 ianuarie 2019 22:42:52
Problema Trie Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <chrono>
#include <random>

using namespace std;

ifstream cin("trie.in");
ofstream cout("trie.out");

unordered_map <string, short int> f;
unordered_map <string, short int> way;

inline void add(string s, int x)
{
    string b;
    for (auto &ch : s)
    {
        b += ch;
        way[b] += x;
    }
    f[b] += x;
}

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

int main()
{
    int type;
    string s;
    while (cin >> type >> s)
    {
        if (type == 0)
        {
            add(s, 1);
        }
        if (type == 1)
        {
            add(s, -1);
        }
        if (type == 2)
        {
            cout << f[s] << "\n";
        }
        if (type == 3)
        {
            int res = 0;
            string b;
            for (auto &ch : s)
            {
                b += ch;
                if (way[b])
                {
                    res = (int) b.size();
                }
            }
            cout << res << "\n";
        }
    }
}