Cod sursa(job #229805)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 11 decembrie 2008 19:45:48
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.06 kb
/***************************************************************************
 *   Copyright (C) 2008 by Pripoae Toni   *
 *   toni@linux-0ztf   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <cassert>
#include <ctime>
#include <string>
#include <algorithm>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <stack>
#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for ((i) = (a); (i) <= (b); ++(i))
#define INF 100000
#define INF2 200000000
#define Nmax 1024
#define LengthMax 27
#define FIN "trie.in"
#define FOUT "trie.out"
#define END 0
using namespace std;
class trie{

private:
    struct Trie{
        int NrSons,Nr;
        Trie *Son[LengthMax];
        /* Here is the constructor of the Trie, to initialize it.*/
        Trie(){
            NrSons = Nr = 0;
            memset(Son,0,sizeof(Son));
        }
    };

public:
    Trie *T;

    void insert(Trie * Now,char * buffer){
        if (*buffer == END){
            Now -> Nr ++;
            return;
        }
        int NowChar = *buffer - 'a';
        if (Now -> Son[NowChar]);
        else{
            Now -> Son[NowChar] = new Trie;
            Now -> NrSons ++;
        }
        insert(Now -> Son[NowChar], buffer + 1);
    }

    bool erase(Trie * Now,char * buffer){
        int NowChar = *buffer - 'a';
        if (*buffer == END)
            Now -> Nr --;
        else if(erase(Now -> Son[NowChar], buffer + 1) == true){
            Now -> Son[NowChar] = 0;
            Now -> NrSons --;
        }
        if (Now != T && Now -> Nr == 0 && Now -> NrSons == 0){
            delete Now;
            return true;
        }
        return false;
    }

    int query(Trie * Now, char * buffer){
        if (*buffer == END)
            return Now -> Nr;
        int NowChar = *buffer - 'a';
        if (Now -> Son[NowChar])
            return query(Now -> Son[NowChar], buffer + 1);
        return 0;
    }

    int prefix(Trie * Now, char * buffer,int Level){
        if (*buffer == END)
            return Level;
        int NowChar = *buffer - 'a';
        if (Now -> Son[NowChar] == 0)
            return Level;
        else
            return prefix(Now -> Son[NowChar], buffer + 1, Level + 1);
    }
    trie(){
        T = new Trie;
    }
};
trie T;
int main(){
    char buffer[30];
    freopen("trie.in","r",stdin);
    freopen("trie.out","w",stdout);
    while (gets(buffer)){
        if (buffer[0] == '0')
            T.insert(T.T,buffer + 2);
        else    if (buffer[0] == '1')
            T.erase(T.T,buffer + 2);
        else    if (buffer[0] == '2')
            printf("%d\n",T.query(T.T,buffer + 2));
        else
            printf("%d\n",T.prefix(T.T,buffer + 2, 0));
    }
}