Cod sursa(job #1654898)

Utilizator vancea.catalincatalin vancea.catalin Data 17 martie 2016 16:31:15
Problema Trie Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
fstream fin("trie.in",ios::in),fout("trie.out",ios::out);
string s;
int maxi;
struct tr
{
    int trm,ct;//cate cuvinte se termina,cate cuvinte trec prin nodul curent
    tr* nxt[26];
    //cout<<"unu\n";
    tr()
    {
        //cout<<"doi\n";
        trm=ct=0;
        memset(nxt,0,sizeof(nxt));
    }
};
void in_out(tr* n,int p,int op)
{
    //(*n).ct++;
    if(op==0)
        (n->ct)++;
    if(op==1)
        (n->ct)--;
    if(op==3 && (n->ct))
        maxi=max(maxi,p);

    if(p==s.size())
    {
        if(op==0)
            (n->trm)++;
        if(op==1)
            (n->trm)--;
        if(op==2)
            fout<<(n->trm)<<"\n";
        if(op==3)
            fout<<maxi<<"\n";
        return ;
    }
    int nx=s[p]-'a';
    if(n->nxt[nx]==NULL)
    {
        n->nxt[nx]=new tr;
    }
    in_out(n->nxt[nx],p+1,op);
}
int main()
{
    int type;
    tr* t=new tr;
    while(fin>>type)
    {
        fin>>s;
        maxi=-9;
        in_out(t,0,type);
    }
}