Pagini recente » Cod sursa (job #399392) | Cod sursa (job #2909767) | Cod sursa (job #1429420) | Cod sursa (job #797905) | Cod sursa (job #1205720)
#include <string.h>
#include <stdio.h>
#define CH (*s - 'a')
struct trie{
int cnt, nr;
trie *fiu[26];
trie(){
cnt=nr=0;
memset(fiu,0,sizeof(fiu));
}
};
trie *T = new trie;
void ins(trie *nod, char *s){
if (*s == '\n'){
nod->cnt++; return;
}
if (nod->fiu[CH] == 0){
nod->fiu[CH] = new trie;
nod->nr++;
}
ins(nod->fiu[CH],s+1);
}
bool del(trie *nod, char *s){
if (*s == '\n')
nod->cnt--;
else if (del(nod->fiu[CH],s+1)){
nod->fiu[CH]=0;
nod->nr--;
}
if (nod->cnt == 0 && nod->nr==0){
delete nod; return 1;
}
return 0;
}
int que(trie *nod, char *s){
if (*s == '\n')
return nod->cnt;
if (nod->fiu[CH])
return que(nod->fiu[CH],s+1);
return 0;
}
int pre(trie *nod, char *s, int k){
if (*s == '\n' || nod->fiu[CH] == 0)
return k;
return pre(nod->fiu[CH],s+1,k+1);
}
int main(){
char line[50];
freopen("trie.in","r",stdin);
freopen("trie.out","w",stdout);
fgets(line,50,stdin);
while (!feof(stdin)){
if (line[0]=='0') ins(T,line+2);
else if (line[0]=='1') del(T,line+2);
else if (line[0]=='2') printf("%d\n",que(T,line+2));
else printf("%d\n",pre(T,line+2,0));
fgets(line,50,stdin);
}
}