Cod sursa(job #1136186)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 8 martie 2014 21:40:30
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.39 kb
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iterator>
#include <random>
#include <assert.h>
using namespace std;

const string file = "trie";

const string infile = file + ".in";
const string outfile = file + ".out";

const int INF = 0x3f3f3f3f; 

//#define ONLINE_JUDGE

struct Trie
{
    int wordCount;
    int fiiCount;
    Trie* fii[26];

    Trie()
    {
        fiiCount = 0;
        wordCount = 0;
        for(int i = 0; i < 26; i++)
        {
            fii[i] = NULL;
        }
    }

    ~Trie()
    {
        for(int i = 0; i < 26; i++)
        {
            if(fii[i] != NULL)
            {
                delete fii[i];
            }
        }
    }

};

void addWord(Trie* root, string& word)
{
    int N = word.length();
    for(int i = 0; i < N; i++)
    {
        int c = word[i] - 'a';
        if(root->fii[c] == NULL)
        {
            root->fii[c] = new Trie();
            root->fiiCount++;
        }
        root = root->fii[c];
    }
    root->wordCount++;
}

bool delWord(Trie* root, string& word, int i)
{
    int N = word.length();
    if(i == N)
    {
        root->wordCount--;
        if(root->fiiCount == 0 && root->wordCount == 0)
        {
            return true;
        }
        return false;
    }

    int c = word[i] - 'a';
    if(delWord(root->fii[c], word, i + 1))
    {
        delete root->fii[c];
        root->fii[c] = NULL;
        root->fiiCount --;
        if(root->fiiCount == 0 && root->wordCount == 0)
        {
            return true;
        }
    }
    return false;
}

void delWord(Trie* root, string& word)
{
    delWord(root, word, 0);
}

int countWord(Trie* root, string& word)
{
    int N = word.length();
    for(int i = 0; i < N; i++)
    {
        int c = word[i] - 'a';
        if(root->fii[c] == NULL)
        {
            return 0;
        }
        root = root->fii[c];
    }
    return root->wordCount;
}

int prefixWord(Trie* root, string& word)
{
    int N = word.length();
    for(int i = 0; i < N; i++)
    {
        int c = word[i] - 'a';
        if(root->fii[c] == NULL)
        {
            return i;
        }
        root = root->fii[c];
    }
    return N;
}


int main()
{
#ifdef ONLINE_JUDGE
	ostream &fout = cout;
	istream &fin = cin;
#else
	fstream fin(infile.c_str(), ios::in);
	fstream fout(outfile.c_str(), ios::out);
#endif	

    Trie* root = new Trie();
    int op;
    string s;
    while((fin >> op) && (fin >> s))
    {
        if(op == 0)
        {

            addWord(root, s);
        }
        else if(op == 1)
        {
            delWord(root, s);
        }
        else if(op == 2)
        {
            fout << countWord(root, s) << "\n";
        }
        else if(op == 3)
        {
            fout << prefixWord(root, s) << "\n";
        }
    }

    delete root;

#ifdef ONLINE_JUDGE
#else
    fout.close();
	fin.close();
#endif
}