Pagini recente » Cod sursa (job #2434962) | Cod sursa (job #623429) | Cod sursa (job #887819) | Cod sursa (job #2957003) | Cod sursa (job #2650477)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
struct Trie{
int words, son;
Trie *fiu[26];
Trie(){
words = son = 0;
memset(fiu, 0, sizeof(fiu));
}
};
Trie *T = new Trie;
void op0(Trie *nod, char *s){
if(*s == '\0'){
nod->words++;
return;
}
else{
nod->son++;
if(nod->fiu[*s - 'a'] == 0)
nod->fiu[*s - 'a'] = new Trie;
op0(nod->fiu[*s-'a'], s+1);
}
}
int op1(Trie *nod, char *s){
if(*s == '\0')
nod->words--;
else if(op1(nod->fiu[*s - 'a'], s+1) == 1){
nod->fiu[*s - 'a'] = 0;
nod->son--;
}
if(nod->words == 0 && nod->son == 0 && nod != T){
delete nod; return 1;
}
return 0;
}
int op2(Trie *nod, char *s){
if(*s == '\0')
return nod->words;
if(nod->fiu[*s - 'a'] == 0)
return 0;
return op2(nod->fiu[*s - 'a'], s+1);
}
int op3(Trie *nod, char *s, int l){
if(*s == '\0' || nod->fiu[*s - 'a'] == 0)
return l;
else
return op3(nod->fiu[*s-'a'], s+1, l+1);
}
int main(){
int op; char s[25];
f>>op>>s;
while(!f.eof()){
switch (op){
case 0: op0(T,s); break;
case 1: op1(T,s); break;
case 2: g<<op2(T,s)<<"\n"; break;
case 3: g<<op3(T,s,0)<<"\n"; break;
}
f>>op>>s;
}
}