Cod sursa(job #1163172)

Utilizator rares96cheseliRares Cheseli rares96cheseli Data 1 aprilie 2014 10:59:37
Problema Trie Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
#define x first
#define y second
#define mp make_pair
using namespace std;

int nrap[100002], nrcuv, v1, v2, tip;
set < pair<string, int> > S;
set < pair<string, int> >::iterator it;
char asd[30];
string cuv;

int comp(string s1, string s2)
{
    int l=min(s1.size(), s2.size()), i=0;
    for (; i<l && s1[i]==s2[i]; ++i);
    return i;
}

int main()
{
    freopen("trie.in", "r", stdin);
    freopen("trie.out", "w", stdout);
    cuv=""; cuv+='z'+1; S.insert(mp(cuv, 0));
    while (!feof(stdin))
    {
        scanf("%d %s", &tip, &asd); cuv=string(asd);
        if (!tip) {
            it=S.lower_bound(mp(cuv, 0));
            if (it->x==cuv) ++nrap[it->y];
                else S.insert(mp(cuv, ++nrcuv)), nrap[nrcuv]=1;
        }

        if (tip==1) {
            it=S.lower_bound(mp(cuv, 0));
            --nrap[it->y];
            if (nrap[it->y]==0) S.erase(it);
        }

        if (tip==2) {
            it=S.lower_bound(mp(cuv, 0));
            if (it->x==cuv) printf("%d\n", nrap[it->y]);
                else printf("0\n");
        }

        if (tip==3){
            it=S.lower_bound(mp(cuv, 0));
            v1=v2=0; v1=comp(it->x, cuv);

            if (it!=S.begin())
                --it, v2=comp(it->x, cuv);
            printf("%d\n", max(v1, v2));
        }
    }
    return 0;
}