Cod sursa(job #1214238)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 29 iulie 2014 21:04:30
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.99 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "trie.in";
const char outfile[] = "trie.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXS = 25;
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int op;
char s[MAXS];

class Trie {
private:
    static const int SIGMA = 26;
    struct Node {
        Node *sons[SIGMA];
        int fr, endi;
        Node() {
            memset(sons, 0, sizeof(sons));
            fr = 0;
            endi = 0;
        }
    } *Root;
public:
    Trie() {
        Root = new Node();
    }
    Node *& getRoot() {
        return this->Root;
    }
    void Insert(Node *& node, char *s) {
        if(!*s) {
            ++ node->endi;
            return;
        }
        int son = *s - 'a';
        if(!node->sons[son])
            node->sons[son] = new Node();
        ++ node->sons[son]->fr;
        Insert(node->sons[son], s + 1);
    }
    void Erase(Node *&node, char *s) {
        if(!*s) {
            -- node->endi;
            return;
        }
        int son = *s - 'a';
        Erase(node->sons[son], s + 1);
        -- node->sons[son]->fr;
        if(!node->sons[son]->fr) {
            delete node->sons[son];
            node->sons[son] = NULL;
        }
    }
    int Freq(Node *&node, char *s) {
        if(!*s)
            return node->endi;
        int son = *s - 'a';
        if(!node->sons[son])
            return 0;
        return Freq(node->sons[son], s + 1);
    }
    int LCP(Node *&node, char *s) {
        if(!*s)
            return 0;
        int son = *s - 'a';
        if(!node->sons[son])
            return 0;
        return 1 + LCP(node->sons[son], s + 1);
    }
} mTrie;

int main() {
    cin.sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);
    #endif
    while(fin >> op >> s) {
        switch (op) {
        case 0:
            mTrie.Insert(mTrie.getRoot(), s);
            break;
        case 1:
            mTrie.Erase(mTrie.getRoot(), s);
            break;
        case 2:
            fout << mTrie.Freq(mTrie.getRoot(), s) << '\n';
            break;
        case 3:
            fout << mTrie.LCP(mTrie.getRoot(), s) << '\n';
            break;
        }
    }
    fin.close();
    fout.close();
    return 0;
}