Pagini recente » Cod sursa (job #1713148) | Cod sursa (job #314125) | Cod sursa (job #2451453) | Cod sursa (job #800315) | Cod sursa (job #3311104)
#include <fstream>
#define nmax (int)(1e5+1)
using namespace std;
ifstream cin("trie.in");
ofstream cout("trie.out");
int task,p,k,l;
char s[22];
struct nod{
int fr[27],cuv,pref;
}t[300001];
void add(int nod){
t[nod].pref++;
if(s[p]==0){
t[nod].cuv++;
return ;
}
if(t[nod].fr[s[p]-'a']==0)
t[nod].fr[s[p]-'a']=++k;
add(t[nod].fr[s[p++]-'a']);
}
void cut(int nod){
t[nod].pref--;
if(s[p]==0){
t[nod].cuv--;
return ;
}
cut(t[nod].fr[s[p++]-'a']);
}
int ap(int nod){
if(s[p]==0)
return t[nod].cuv;
if(t[nod].fr[s[p]-'a']!=0)
return ap(t[nod].fr[s[p++]-'a']);
return 0;
}
void lg(int nod){
if(t[nod].pref<=0)
return ;
l=p;
if(s[p]==0)
return ;
if(t[nod].fr[s[p]-'a']!=0){
lg(t[nod].fr[s[p++]-'a']);
}
}
int main()
{
while(cin>>task>>s){
p=0;
if(task==0)
add(0);
else if(task==1)
cut(0);
else if(task==2)
cout<<ap(0)<<'\n';
else{
l=0;
lg(0);
cout<<l<<'\n';
}
}
return 0;
}