Pagini recente » Monitorul de evaluare | Cod sursa (job #1512658) | Cod sursa (job #1044964) | Cod sursa (job #204630) | Cod sursa (job #1133966)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("trie.in");
ofstream g("trie.out");
char c[35],ch;
int len;
struct Trie
{ int words,freq;
Trie *son[27];
Trie()
{ int i;
for(i=0;i<=26;i++)
son[i]=NULL;
words=0; freq=0;
}
};
Trie *beg = new Trie;
void Insert(Trie *nod)
{ int i,next;
for(i=0;i<len;i++)
{ next=c[i]-'a';
if (nod->son[next]==NULL)
nod->son[next] = new Trie;
nod = nod->son[next];
nod->freq++;
}
nod->words++;
}
void Delete(Trie *nod)
{ int i,next;
for(i=0;i<len;i++)
{ next=c[i]-'a';
nod = nod->son[next];
nod->freq--;
}
nod->words--;
}
int Search(Trie *nod)
{ int i,next;
for(i=0;i<len;i++)
{ next=c[i]-'a';
if (nod->son[next]==NULL)
return 0;
nod = nod->son[next];
}
return nod->words;
}
int Prefix(Trie *nod)
{ int i,next,sol=0;
for(i=0;i<len;i++)
{ next=c[i]-'a';
if (nod->son[next]==NULL)
break;
nod = nod->son[next];
if (nod->freq>0) sol++;
else break;
}
return sol;
}
int main()
{ int t;
while(f>>t>>c)
{ len=strlen(c);
if (t==0) Insert(beg);
if (t==1) Delete(beg);
if (t==2) g<<Search(beg)<<"\n";
if (t==3) g<<Prefix(beg)<<"\n";
}
return 0;
}