Cod sursa(job #1945750)

Utilizator dumitrescugeorgeGeorge Dumitrescu dumitrescugeorge Data 29 martie 2017 17:39:03
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct trie{
    int ap;
    int nrc;
    trie *copii[31];
    trie()
    {ap=0;nrc=0; for(int i=0;i<=30;i++)
    copii[i]=0;
    }
}*root;
void ins(trie *&root,char *p)
{
    if(!*p)
    {
        root->ap++;
        return ;
    }
    if(root->copii[*p-'a'])
        {
            ins(root->copii[*p-'a'],p+1);
            return;
        }
    root->copii[*p-'a']=new trie;
    ins(root->copii[*p-'a'],p+1);
}
void delete1(trie *&root,char *p)
{
    if(!*p)
    {
        root->ap--;
        return ;
    }
    delete1(root->copii[*p-'a'],p+1);
}
void tipar(trie *&root,char *p)
{
    if(!*p)
        {
            printf("%d\n",root->ap);
            return;
        }
    tipar(root->copii[*p-'0'],p+1);
}
void citire()
{
    int x;
    root=new trie;
    char s[200];
    int y;
    while(y>0)
    {
        y=scanf("%d ",&x);
        scanf("%s",s);
        char *p=s;
        if(x==0)
            ins(root,p);
        else if(x==1)
            delete1(root,p);
        else if(x==2)
            tipar(root,p);
    }
}
int main()
{
    freopen("trie.in","r",stdin);
    freopen("trie.out","w",stdout);
    citire();
    //cout << "Hello world!" << endl;
    return 0;
}