Pagini recente » Cod sursa (job #2248666) | Cod sursa (job #2728389) | Cod sursa (job #3165813) | Cod sursa (job #419595) | Cod sursa (job #1655388)
#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));
}
};
tr* t=new tr;
void in_out(tr* pre,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";
if(n->trm==0 && n->ct==0 && n!=t)
{
pre->nxt[s[p-1]-'a']=NULL;
delete n;
}
return ;
}
int nx=s[p]-'a';
if(n->nxt[nx]==NULL)
n->nxt[nx]=new tr;
in_out(n,n->nxt[nx],p+1,op);
if(n->trm==0 && n->ct==0 && n!=t)
{
pre->nxt[s[p-1]-'a']=NULL;
delete n;
}
}
int main()
{
int type;
while(fin>>type)
{
fin>>s;
maxi=0;
in_out(t,t,0,type);
}
}