Pagini recente » Cod sursa (job #2732858) | Cod sursa (job #1186549) | Cod sursa (job #3199362) | Cod sursa (job #2346158) | Cod sursa (job #2648480)
#include <fstream>
#include <cstring>
using namespace std;
ifstream cin("trie.in");
ofstream cout("trie.out");
struct Trie{
Trie *f[28];
int cnt, nf;
Trie() {
cnt=nf=0;
memset(f,0,sizeof(f));
}
};
Trie * T= new Trie;
inline void add(char ch[])
{
char *p=ch;
Trie *t=T;
while(*p!='\0')
{
if(t->f[*p-'a']==0)
{
t->f[*p-'a']=new Trie;
t->nf++;
}
t=t->f[*p-'a'];
++p;
}
t->cnt++;
}
inline int ext(Trie * el, char *p)
{
if(*p=='\0')
el->cnt--;
else if(ext(el->f[*p-'a'],p+1)){
el->f[*p-'a']=0;
el->nf--;
}
if(el->nf==0 && el->cnt==0 && el!=T)
{
delete el;
return 1;
}
return 0;
}
inline int que1(char ch[])
{
char *p=ch;
Trie *t=T;
while(*p!='\0')
{
if(t->f[*p-'a']==0)
return 0;
t=t->f[*p-'a'];
p++;
}
return (t->cnt);
}
inline int que2(char ch[])
{
char *p=ch;
Trie *t=T;
int k=0;
while(*p!='\0')
{
if(t->f[*p-'a']==0)
return k;
t=t->f[*p-'a'];
p++; k++;
}
return k;
}
int main()
{
int N, op;
char str[30];
while(cin>>op>>str)
{
if(op==0)
add(str);
else if(op==1)
{
char *poi=str;
ext(T,poi);
}
else if(op==2)
cout<<que1(str)<<'\n';
else cout<<que2(str)<<'\n';
}
return 0;
}