Pagini recente » Cod sursa (job #2025009) | Cod sursa (job #2557543) | Cod sursa (job #3160056) | Cod sursa (job #1418966) | Cod sursa (job #2433991)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
typedef string::iterator string_it;
struct trie{
int nr,nrchild;
trie *child[27];
trie(){
nr=nrchild=0;
memset(child,0,sizeof(child));
}
};
trie* T = new trie;
int lg;
string s;
void Add(trie*, string_it);
void Del(trie*, string_it);
int Cnt(trie*, string_it);
void Lg(trie*, string_it);
int main(){
short task;
while(fin>>task>>s){
switch(task){
case 0:
Add(T, s.begin());
break;
case 1:
Del(T, s.begin());
break;
case 2:
fout<<Cnt(T, s.begin())<<'\n';
break;
case 3:
lg=0;
Lg(T,s.begin());
fout<<lg<<'\n';
break;
}
}
return 0;
}
void Lg(trie* t, string_it it){
char ch=*it-'a';
if(it==s.end() || t->child[ch] == false)
return;
++lg;
Lg(t->child[ch], it+1);
}
int Cnt(trie* t, string_it it){
char ch=*it-'a';
if(it==s.end())
return t->nr;
if(t->child[ch] == false)
return 0;
return Cnt(t->child[ch], it+1);
}
void Del(trie* t, string_it it){
if(it==s.end()){
--t->nr;
return;
}
char ch=*it-'a';
Del(t->child[ch], it+1);
if(t->child[ch]->nrchild == 0 && t->child[ch]->nr == 0){
--(t->nrchild);
delete t->child[ch];
t->child[ch]=0;
}
}
void Add(trie* t, string_it it){
if(it==s.end()){
++t->nr;
return;
}
char ch=*it-'a';
// if(t->child[ch] && t->child[ch]->nrchild == 0 && t->child[ch]->nr == 0){
// delete t->child[ch];
// }
if(t->child[ch] == false){
t->child[ch]= new trie();
++t->nrchild;
}
Add(t->child[ch], it+1);
}