Pagini recente » Cod sursa (job #2730090) | Cod sursa (job #1710421) | Cod sursa (job #2792220) | Cod sursa (job #2064624) | Cod sursa (job #3198464)
#include <fstream>
#include <string>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
int tip,crt,ok,lg;
const int sz=2000000;
char x[30];
struct nod
{
int pref;
int cuv;
int fr[26];
}trie[sz+5];
void insert(char* st,int nodc)
{
trie[nodc].pref++;
if(st[0]==0)
trie[nodc].cuv++;
else
{
if(trie[nodc].fr[st[0]-'a'])
insert(st+1,trie[nodc].fr[st[0]-'a']);
else
{
trie[nodc].fr[st[0]-'a']=++crt;
insert(st+1,crt);
}
}
}
void del(char* st,int nodc)
{
trie[nodc].pref--;
if(st[0]==0)
trie[nodc].cuv--;
else
{
del(st+1,trie[nodc].fr[st[0]-'a']);
}
/*if(trie[trie[nodc].fr[st[0]-'a']].pref==0)
trie[nodc].fr[st[0]-'a']=0;*/
}
void caut(char* st,int nodc)
{
if(st[0]==0){
fout<<trie[nodc].cuv<<"\n";
return;
}
if(trie[nodc].fr[st[0]-'a']!=0)
{
caut(st+1,trie[nodc].fr[st[0]-'a']);
}
else
fout<<0<<"\n";
}
void caut2(char* st,int nodc,int l)
{
if(trie[nodc].pref>0)
{
lg=l;
if(st[0]==0){
return;
}
if(trie[nodc].fr[st[0]-'a']!=0)
caut2(st+1,trie[nodc].fr[st[0]-'a'],l+1);
}
}
int main()
{
while(fin>>tip>>x)
{
if(tip==0)
{
insert(x,0);
}
else if(tip==1)
{
del(x,0);
}
else if(tip==2)
{
caut(x,0);
}
else
{
lg=0;
caut2(x,0,0);
fout<<lg<<"\n";
}
}
return 0;
}