Pagini recente » Cod sursa (job #2532355) | Cod sursa (job #1287959) | Cod sursa (job #2910897) | Cod sursa (job #1878636) | Cod sursa (job #2161277)
#include <bits/stdc++.h>
using namespace std;
struct trie{
int cnt, nrfii, here;
trie *f[30];
trie(){
cnt = nrfii = here = 0;
memset(f, 0, sizeof(f));
}
};
int test, n;
trie *rad = new trie;
char c[25];
int main()
{
ifstream fin ("trie.in");
ofstream fout ("trie.out");
while(fin >> test){
fin >> c;
n = strlen(c);
trie *t;
bool ok;
switch(test){
case 0:
t = rad;
for (int i = 0; i < n; ++i){
int q = c[i]-'a';
if(t->f[q] == NULL){
++t->nrfii;
t->f[q] = new trie;
++t->f[q]->cnt;
}else ++t->f[q]->cnt;
t = t->f[q];
}
++t->here;
break;
case 1:
t = rad;
ok = false;
for (int i = 0; i < n; ++i){
int q = c[i]-'a';
if(t->f[q]->cnt == 1){
delete t->f[q];
t->f[q] = NULL;
--t->nrfii;
ok = true;
break;
}
--t->f[q]->cnt;
t = t->f[q];
}
if(!ok)
--t->here;
break;
case 2:
t = rad;
ok = false;
for (int i = 0; i < n; ++i){
int q = c[i]-'a';
if(!t->f[q]){
ok = true;
break;
}
t = t->f[q];
}
if(ok)
fout << "0\n";
else fout << t->here << "\n";
break;
case 3:
t = rad;
ok = false;
for (int i = 0; i < n; ++i){
int q = c[i]-'a';
if(!t->f[q]){
ok = true;
fout << i << "\n";
break;
}
t = t->f[q];
}
if(!ok)
fout << n << "\n";
break;
}
}
return 0;
}