Cod sursa(job #1975855)

Utilizator DobosDobos Paul Dobos Data 2 mai 2017 11:50:06
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>
#define C (*s - 'a')

using namespace std;

ifstream fin("Input");


struct trie{
    int wo,pf;
    trie *edge[26];
    char place[10];
    trie(){
        wo = pf = 0;
        memset (edge,0,sizeof( edge ));
    }

};

trie *T = new trie;


void Edit(char S[32],char &W[32],char &A[32]){
    j = 0;
    for(int i = 0; S[i] != '\000' && !isdigit(S[i]) ;i++){
        if(i == ' ') continue;
        if(S[i] >= 'A' && S[i] <= 'Z')
            S[i] -= 32;
        W[j++] = S[i];

    }

}



void addw(trie *nod, char *s){
    if(*s == '\000'){
        nod ->wo++; return;
    }
    if(nod -> edge[C] == 0){
        nod -> edge[C] = new trie;
        nod -> pf ++;
    }
    addw(nod ->edge[C], s + 1);
}

int deletew(trie *nod,char *s){
     if(*s == '\000'){
        nod ->wo--;
    } else if(deletew(nod ->edge[C],s + 1)){
        nod ->edge[C] = 0;
        nod ->pf --;
    }
    if(nod->wo == 0 && nod ->pf == 0 && nod != T){
        delete nod; return 1;
    }
    return 0;
}

int queryw(trie *nod,char *s){
     if(*s == '\000')
        return nod ->wo;
    if(nod ->edge[C])
        return queryw(nod ->edge[C], s + 1);
    return 0;
}

int main()
{
    ios :: sync_with_stdio(false);
    char S[32],W[32],A[32];
    while(fin.getline(S,sizeof(S))){
        Edit(S,A,S);
        addw(T,A,W);
        if(S[0] == '1')
            deletew(T,S + 2);
        if(S[0] == '2')
           fout << queryw(T,S + 2) << "\n";

    }
    while(cin.getline (S,sizeof(S))){

        if(S[0] == '0')
            addw(T,S + 2);
        if(S[0] == '1')
            deletew(T,S + 2);
        if(S[0] == '2')
           fout << queryw(T,S + 2) << "\n";


    }

    return 0;
}