Cod sursa(job #2422417)

Utilizator MortemPlaiasu Iulia-Silvia Mortem Data 18 mai 2019 17:42:33
Problema Trie Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#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 );
	}
}