Pagini recente » Cod sursa (job #373995) | Cod sursa (job #851157) | Cod sursa (job #1647151) | Cod sursa (job #1227461) | Cod sursa (job #2422417)
#include <iostream>
#include <cstdio>
#include <cstring>
#define ch (c[ind] - 'a')
char c[32];
struct nod
{
int pre=0,app=0;
nod* v[26]={NULL};
};
void push( nod* root,int ind)
{
if(root->v[ch]==NULL)
root->v[ch]= new nod;
root->v[ch]->pre++;
if(c[ind+1]!='\n')
push(root->v[ch],ind+1);
else
root->v[ch]->app++;
//std::cout<<c<<" "<<(char)(ch+'a')<<" "<<root->v[ch]->pre<<" "<<root->v[ch]->app<<"\n";
}
void erase(nod* root, int ind)
{
root->v[ch]->pre--;
if(c[ind+1]=='\n')
root->v[ch]->app--;
else
erase(root->v[ch],ind+1);
if(root->v[ch]->pre==0)
{
delete root->v[ch];
root->v[ch]=NULL;
}
//std::cout<<c<<" "<<(char)(ch+'a')<<" "<<root->v[ch]->pre<<" "<<root->v[ch]->app<<"\n";
}
int find(nod *root, int ind)
{
if(c[ind+1]=='\n' && root->v[ch]!=NULL)
return root->v[ch]->app;
else if(root->v[ch]!=NULL)
return find(root->v[ch],ind+1);
else
return 0;
}
int larg(nod *root, int ind, int countt)
{
if(c[ind+1]=='\n'&& root->v[ch]!=NULL && root->v[ch]->pre!=0)
return countt+1;
else if(root->v[ch]!=NULL && root->v[ch]->pre!=0)
return larg(root->v[ch],ind+1,countt+1);
else
return countt;
}
nod * root = new nod;
int main()
{
freopen( "trie.in", "r", stdin );
freopen( "trie.out", "w", stdout );
fgets( c, 32, stdin );
while( !feof( stdin ) )
{
if(c[0]=='0')
push( root, 2 );
else if(c[0]=='1')
erase( root, 2 );
else if(c[0]=='2')
printf( "%d\n", find( root, 2 ) );
else if(c[0]=='3')
printf( "%d\n", larg( root, 2, 0 ) );
// std::cout<<c;
fgets( c, 32, stdin );
}
}