Pagini recente » Cod sursa (job #2526750) | Cod sursa (job #535983) | Cod sursa (job #582710) | Cod sursa (job #942888) | Cod sursa (job #2137826)
#include <bits/stdc++.h>
using namespace std;
struct Trie{
int nr, cnt;
Trie *fiu[26];
Trie(){
nr = 0; cnt = 0;
memset(fiu, 0, sizeof(fiu));
}
};
Trie *T = new Trie;
inline void add(Trie *nod, char *s){
if(*s == NULL) {
nod->cnt++;
return ;
}
if(nod->fiu[*s - 'a'] == 0){
nod->fiu[*s - 'a'] = new Trie;
nod->nr++;
}
add(nod->fiu[*s - 'a'], s + 1);
}
inline bool del(Trie *nod, char *s){
if(*s == NULL) nod->cnt--;
else if(del(nod->fiu[*s - 'a'], s + 1)){
nod->nr--;
nod->fiu[*s - 'a'] = 0;
}
if(nod->cnt == 0 && nod != T && nod->nr == 0){
delete nod;
return 1;
}
return 0;
}
inline int ap(Trie *nod, char *s){
if(*s == NULL) return nod->cnt;
if(nod->fiu[*s - 'a'] == NULL) return 0;
return ap(nod->fiu[*s - 'a'], s + 1);
}
inline int pref(Trie *nod, char *s, int k){
if(*s == NULL) return k;
if(nod->fiu[*s - 'a'] == NULL) return k;
return pref(nod->fiu[*s - 'a'], s + 1, k + 1);
}
char s[100005];
int main(){
freopen("trie.in", "r", stdin);
freopen("trie.out", "w", stdout);
int k;
while(!feof(stdin)){
scanf("%d", &k); scanf("%s\n", s);
if(k == 0) add(T, s);
else if(k == 1) del(T, s);
else if(k == 2) printf("%d\n", ap(T, s));
else if(k == 3) printf("%d\n", pref(T, s, 0));
}
return 0;
}