Cod sursa(job #1697018)

Utilizator razvandRazvan Dumitru razvand Data 30 aprilie 2016 15:39:25
Problema Trie Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

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

typedef trie<
    string,
    null_type,
    trie_string_access_traits<>,
    pat_trie_tag,
    trie_prefix_search_node_update>
pref_trie;

unordered_map<string, int> mp;

int main() {

    int T,n,m,t;
    char Q;
    string x;

    pref_trie base;

    while(!in.eof()) {

        in >> T >> x;

        if(in.eof())
            break;

        if(T == 0) {
            base.insert(x);
            mp[x]++;
        } else if(T == 1) {
            base.erase(x);
            mp[x]--;
        } else if(T == 2) {
            out << mp[x] << '\n';
        } else {
            int i = 0;
            for(i = 0; i < x.size(); i++) {
                auto range = base.prefix_range(x.substr(0, i));
                if(range.first == range.second)
                    break;
            }
            out << i-1 << '\n';
        }

    }

}