Pagini recente » Cod sursa (job #2912363) | Cod sursa (job #482920) | Cod sursa (job #2308129) | Cod sursa (job #1749602) | Cod sursa (job #244677)
Cod sursa(job #244677)
#include <stdio.h>
#include <string.h>
long t,l,q=1,ok; char ch[32];
struct trie{int f[26];int cuv;int son;};
trie next[120002];
void add(int nod, char ch[32]){
if (ch[0]==0){next[nod].cuv++;return;}
long k=ch[0]-'a';
if (next[nod].f[k]==0){next[nod].f[k]=++q; next[nod].son++;}
add(next[nod].f[k],ch+1);
}
int del(int nod, char ch[32]){
if (ch[0]==0) next[nod].cuv--;
else
if (del(next[nod].f[ch[0]-'a'],ch+1)){
next[nod].f[ch[0]-'a']=0;
next[nod].son--;
}
if (next[nod].cuv==0 && next[nod].son==0 && nod!=1)return 1;
return 0;
}
int count(int nod, char ch[32]){
if (ch[0]==0)return next[nod].cuv;
if (next[nod].f[ch[0]-'a'])return count(next[nod].f[ch[0]-'a'],ch+1);
return 0;
}
int prefix(int nod, int l,char ch[32]){
if (ch[0]==0 || next[nod].f[ch[0]-'a']==0)return l;
return prefix(next[nod].f[ch[0]-'a'],l+1,ch+1);
}
int main(){
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
while(1){
t=-1; scanf("%ld %s\n",&t,ch); if (t==-1)break;
if (t==0)add(1,ch);
if (t==1)del(1,ch);
if (t==2)printf("%ld\n",count(1,ch));
if (t==3)printf("%ld\n",prefix(1,0,ch));
}
return 0;
}