Cod sursa(job #2982292)

Utilizator superffffalexandru radu superffff Data 19 februarie 2023 21:17:08
Problema Trie Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.07 kb
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cctype>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream in("trie.in");
ofstream out("trie.out");
int a,b,c,d,e,f,g,i,j,k,l,m,n,o,q,s,S,t,T,x,y,z,ok,nr,w,C,k1,k2,poz,pas,Max,mid,nways,MOD;
const int SIGMA=27;
char str[25];
struct Trie{
   Trie* children[SIGMA];
   int isWord,nrc;
};
Trie *root;
Trie *r;
Trie* newTrie(){
   Trie* answer=new Trie();
   answer->isWord = 0;
   answer->nrc= 0;
   for(i=0;i<SIGMA;i++){
    answer->children[i]=NULL;
   }
   return answer;
}
void Insert(Trie *root, char *S){
  if(S[0]== '\0'){
    root->isWord++;
  }
  else{
    if(root->children[S[0]-'a']==NULL){
        root->children[S[0]-'a'] = newTrie();
        root->nrc++;
    }
    Insert(root->children[S[0]-'a'],S+1);
  }
}
int Erase(Trie *root,char *S){
   if(S[0]== '\0'){
    root->isWord--;
   }
   else{
   if( Erase(root->children[S[0]-'a'],S+1)){
         root->nrc--;
         root->children[S[0]-'a']=0;
   }
   }
   if(root->isWord==0 && root->nrc==0 && root!=r){
    delete root; return 1;
   }
   return 0;

}
int Find(Trie *root,char *S){
   if(S[0]=='\0'){
    return root->isWord;
   }
   if(root->children[S[0]-'a']==NULL){
    return 0;
   }
   else return Find(root->children[S[0]-'a'],S+1);
}
int FindPref(Trie *root,char *S,int lvl){
    if(S[0]=='\0'){
        return lvl;
    }
    if(root->children[S[0]-'a']==NULL){
        return lvl;
    }
    return FindPref(root->children[S[0]-'a'],S+1,lvl+1);

}
int main()
{
    root = newTrie();
    r=newTrie();
 while(in>>x){
        in>>str;
    if(x==0){
        Insert(root,str);
    }
    if(x==1){
    Erase(root,str);
     }
     if(x==2){
        out<<Find(root,str);
        out<<'\n';
     }
     if(x==3){
            k=0;
            Max=0;
           out<<FindPref(root,str,k);
        out<<'\n';
     }

 }
   return 0;
}