Pagini recente » Cod sursa (job #1648570) | Cod sursa (job #2094448) | Cod sursa (job #2425556) | Cod sursa (job #2396757) | Cod sursa (job #2422514)
#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 write(nod * root)
{
for(int i=0;i<26;i++)
if(root->v[i]!=NULL)
std::cout<<(char)(i+'a')<<" "<<root->v[i]->pre<<" "<<root->v[i]->app<<" ";
std::cout<<"\n";
for(int i=0;i<26;i++)
if(root->v[i]!=NULL)
write(root->v[i]);
}
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++;*/
while(c[ind+1]!='\n')
{
if(root->v[ch]==NULL)
root->v[ch]=new nod;
root->v[ch]->pre++;
// std::cout<<c<<" "<<c[ind]<<" "<<root->v[ch]->pre<<" "<<root->v[ch]->app<<"\n";
root=root->v[ch];
ind++;
}
if(root->v[ch]==NULL)
root->v[ch]=new nod;
root->v[ch]->pre++;
root->v[ch]->app++;
// std::cout<<c<<" "<<c[ind]<<" "<<root->v[ch]->pre<<" "<<root->v[ch]->app<<"\n";
}
void erase(nod* root, int ind)
{
root->v[ch]->pre--;
// std::cout<<c<<" "<<c[ind]<<" "<<root->v[ch]->pre<<" "<<root->v[ch]->app<<"\n";
if(c[ind+1]=='\n')
root->v[ch]->app--;
else
erase(root->v[ch],ind+1);
if(root->v[ch]->pre==0)
{
// std::cout<<"del:"<<c<<"\n";
for(int i=0;i<26;i++)
delete root->v[ch]->v[i];
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)
{
while(c[ind+1]!='\n')
{
if(root->v[ch]==NULL)
return 0;
root= root->v[ch];
ind++;
}
if(root->v[ch]!=NULL)
return root->v[ch]->app;
return 0;
}
int larg(nod *root, int ind, int countt)
{
while(c[ind]!='\n')
{
if(root->v[ch]==NULL || root->v[ch]->pre==0)
return countt;
root=root->v[ch];
ind++;
countt++;
}
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 );
}
}