Cod sursa(job #788758)

Utilizator round2Test P round2 Data 15 septembrie 2012 19:59:44
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <cstdio>
#include <cctype>
#include <cstring>
struct trie{
    struct trie*f[26];
    int p,c;
    trie(){
        memset(f,0,sizeof(f));
        p=c=0;} }*t;
char a[22];

void add(char *b)
{
    trie *g=t;
    int urm;
    while(isalpha(*b))
    {
        urm=*b-'a';
        if(g->f[urm]==0)g->f[urm]=new trie;
        g=g->f[urm];
        g->p++;
        b++;
    }
        g->c++;
}

void rem(char *b)
{
    trie *g=t;
    int urm;
    while(isalpha(*b))
    {
        urm=*b-'a';
        g=g->f[urm];
        g->p--;
        b++;
    }
        g->c--;
}

int ap(char *b)
{
    trie *g=t;
    int urm;
    while(isalpha(*b))
    {
        urm=*b-'a';
        if(g->f[urm]==0)return 0;
        g=g->f[urm];
        b++;
    }
    return g->c;
}

int pr(char *b)
{
    trie *g=t;
    int urm,l=0;
    while(isalpha(*b))
    {
        urm=*b-'a';
        if(g->f[urm]==0)return l;
        g=g->f[urm];
        if(g->p==0)return l;
        l++;
        b++;
    }
    return l;
}

int main()
{
    int c;
    freopen("trie.in","r",stdin);
    freopen("trie.out","w",stdout);
    t=new trie;
    while(scanf("%d %s ",&c,a)!=-1)
    {
        switch(c)
        {
            case 0: add(a); break;
            case 1: rem(a); break;
            case 2: printf("%d\n",ap(a)); break;
            case 3: printf("%d\n",pr(a)); break;
        }
    }
    return 0;
}