Pagini recente » Cod sursa (job #1996061) | Cod sursa (job #1060205) | Cod sursa (job #749082) | Cod sursa (job #2282230) | Cod sursa (job #2509117)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct trie {
trie *c[26];
int nrap, endingHere;
trie() {
for(int i = 0; i < 26; i++)
c[i] = NULL;
nrap = endingHere = 0;
}
} *start = new trie;
int op, l;
char cuv[25];
void add_cuv(int dim) {
trie *cnod = new trie;
cnod = start;
for(int i = 0; i < dim; i++) {
if(cnod->c[cuv[i] - 'a'] == NULL)
cnod->c[cuv[i] - 'a'] = new trie;
cnod = cnod->c[cuv[i] - 'a'];
cnod->nrap++;
}
cnod->endingHere++;
}
void delete_cuv(int dim) {
trie *cnod = new trie;
cnod = start;
for(int i = 0; i < dim; i++) {
cnod = cnod->c[cuv[i] - 'a'];
cnod->nrap--;
}
cnod->endingHere--;
}
void print_ap(int dim) {
trie *cnod = new trie;
cnod = start;
int i = 0;
while(i < dim && cnod->c[cuv[i] - 'a'] != NULL) {
cnod = cnod->c[cuv[i] - 'a'];
i++;
}
if(i == dim)
printf("%d\n", cnod->endingHere);
}
void prefix(int dim) {
trie *cnod = new trie;
cnod = start;
int maxl = 0;
int i = 0;
while(i < dim && cnod->c[cuv[i] - 'a'] != NULL) {
cnod = cnod->c[cuv[i] - 'a'];
if(cnod->nrap > 0)
maxl = i + 1;
i++;
}
printf("%d\n", maxl);
}
int main() {
freopen("trie.in", "r", stdin);
freopen("trie.out", "w", stdout);
char c;
while(!feof(stdin)) {
scanf("%d ", &op);
fgets(cuv, 22, stdin);
l = strlen(cuv);
cuv[l - 1] = NULL;
l--;
if(!feof(stdin))
if(op == 0)
add_cuv(l);
else if(op == 1)
delete_cuv(l);
else if(op == 2)
print_ap(l);
else if(op == 3)
prefix(l);
}
return 0;
}