Pagini recente » Cod sursa (job #793523) | Cod sursa (job #669501) | Cod sursa (job #841482) | Cod sursa (job #3270820) | Cod sursa (job #3271493)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("trie.in");
ofstream fout("trie.out");
char s[255];
struct Trie{
int nrFii,cnt;
Trie *fiu[26];
Trie(){
nrFii=cnt=0;
for(int i=0;i<26;++i){
fiu[i]=0;
}
}
};
Trie *t = new Trie;
void ins( Trie *nod, char *s ) {
if( s[0] == '\0' ) {
nod->cnt ++; return;
}
if( nod->fiu[ s[0]-'a' ] == 0 ) {
nod->fiu[ s[0]-'a' ] = new Trie;
nod->nrFii ++;
}
ins( nod->fiu[ s[0]-'a' ], s+1 );
}
int del( Trie *nod, char *s ) {
if( s[0] == '\0' )
nod->cnt --;
else if( del( nod->fiu[ s[0]-'a' ], s+1 ) ) {
nod->fiu[ s[0]-'a' ] = 0;
nod->nrFii --;
}
if( nod->cnt == 0 && nod->nrFii == 0 && nod!=t) {
delete nod; return 1;
}
return 0;
}
int afis( Trie *nod, char *s ) {
if( s[0] == '\0' )
return nod->cnt;
if( nod->fiu[ s[0]-'a' ] )
return afis( nod->fiu[ s[0]-'a' ], s+1 );
return 0;
}
int prefix( Trie *nod, char *s, int m ) {
if( s[0] == '\0')
return m;
if( nod->fiu[ s[0]-'a' ] == 0 )
return m;
return prefix( nod->fiu[ s[0]-'a' ], s+1, m+1 );
}
int main(){
while(fin.getline(s, 255)){
if(s[0]=='0'){
ins(t,s+2);
}else if(s[0]=='1'){
del(t,s+2);
}
else if(s[0]=='2'){
fout<<afis(t,s+2)<<endl;
}
else if(s[0]=='3'){
fout<<prefix(t,s+2,0)<<endl;;
}
}
}