Cod sursa(job #2311979)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 3 ianuarie 2019 22:42:17
Problema Trie Scor 55
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 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, int> f;
unordered_map <string, 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 a[4];
    a[0] = min(rand(), 100000000); a[1] = min(a[1], 1000);
    a[2] = min(rand(), 100000000); a[3] = min(a[1], 1000);
    f.rehash(max(1000000, a[0] % a[1]));
    way.rehash(max(1000000, a[2] % a[3]));
    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";
        }
    }
}