Cod sursa(job #1194641)

Utilizator RynaquiAxinte Silviu Rynaqui Data 4 iunie 2014 14:04:47
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <cstdio>

using namespace std;
struct trie
{
    int cnt;
    int cnt2;
    trie *s[26];
    trie()
    {
        cnt=cnt2=0;
        for(int i=0;i<26;i++)
            s[i]=0;
    }
};
char w[25],*p;
int c;
trie *root,*pt;
int main()
{
    freopen("trie.in","r",stdin);
    freopen("trie.out","w",stdout);
    root=new trie;
    for(;scanf("%d",&c)+1;)
    {
        scanf("%s",w);
        if(c==0)
        {
            for(p=w,pt=root;*p;p++)
            {
                if(!pt->s[*p-'a'])
                    pt->s[*p-'a']=new trie;
                pt=pt->s[*p-'a'];pt->cnt2++;
            }
            pt->cnt++;

        }
        else
        if(c==1)
        {
            for(p=w,pt=root;*p;p++)
            {
                pt=pt->s[*p-'a'];
                pt->cnt2--;
            }
            pt->cnt--;

        }
        else
        if(c==2)
        {
            for(p=w,pt=root;*p;p++)
            {
                if(!pt->s[*p-'a']){pt=0;break;}
                pt=pt->s[*p-'a'];
            }
            if(!pt)printf("0\n");
            else printf("%d\n",pt->cnt);
        }
        else
        {
            int lg=0;
            for(p=w,pt=root;*p;p++)
            {
                if(!pt->s[*p-'a'])break;
                if(!pt->s[*p-'a']->cnt2)break;
                pt=pt->s[*p-'a'];lg++;
            }
            printf("%d\n",lg);
        }
    }
    return 0;
}