Cod sursa(job #1464989)

Utilizator TeodorescuStefanEduardTeodorescu Stefan Eduard TeodorescuStefanEduard Data 26 iulie 2015 11:37:23
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.71 kb
#ifdef ONLINE_JUDGE
    #include <bits/stdc++.h>
#else
    #include <algorithm>
    #include <bitset>
    #include <cassert>
    #include <cmath>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <map>
    #include <set>
    #include <stack>
    #include <string>
    #include <utility>
    #include <vector>
    #include <queue>
    #include <climits>
#endif
 
using namespace std;
 
    // lambda : [] (int a, int b) -> bool { body return }
    // string r_str = R"(raw string)"
 
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define CRLINE Duxar(__LINE__);
#define SHOWME(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl;
#define ENTER putchar('\n');
 
int dx4[] = {-1, 0, 1, 0};
int dy4[] = {0, 1, 0, -1};
 
int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1};
 
void Duxar(int _this_line) {
#ifndef ONLINE_JUDGE
    printf("\n . . . . . . . . . . . . . Passed line - %d\n", _this_line);
#endif
}
 
template <class T>
void ReadNo(T &_value) {
    T _sign = 1;
    char ch;
    _value = 0;
    while(!isdigit(ch = getchar())) {
        (ch == '-') && (_sign = -1);
    }
    do {
        _value = _value * 10 + (ch - '0');
    } while(isdigit(ch = getchar()));
    _value *= _sign;
}
 
class Trie {
public:
    int went_here, end_here;
    vector <Trie *> letter;
     
    Trie() {
        end_here = went_here = 0;
        letter.resize(26, 0);
    }
     
    void insert_word(char *word) {
        went_here++;
        if (!isalpha(word[0])) {
            end_here++;
            return;
        }
        int to = word[0] - 'a';
        if (letter[to] == 0) {
            letter[to] = new Trie();
        }
        letter[to]->insert_word(word + 1);
    }
     
    void delete_word(char *word) {
        --went_here;
        if (!isalpha(word[0])) {
            --end_here;
            return;
        }
        int to = word[0] - 'a';
        letter[to]->delete_word(word + 1);
        if (letter[to]->went_here == 0) {
            delete letter[to];
            letter[to] = 0;
        }
    }
     
    int get_went_here(char *word) {
        if (!isalpha(word[0])) {
            return end_here;
        }
        int to = word[0] - 'a';
        if (letter[to] == 0) {
            return 0;
        }
        return letter[to]->get_went_here(word + 1);
    }
     
    int get_longest_prefix(char *word, int lg) {
        if (!isalpha(word[0])) {
            return lg;
        }
        int to = word[0] - 'a';
        if (letter[to] == 0) {
            return lg;
        }
        return letter[to]->get_longest_prefix(word + 1, lg + 1);
    }
};
 
int main(){
    string file_input = "trie";
#ifdef INFOARENA
    freopen((file_input + ".in").c_str(), "r", stdin);
    freopen((file_input + ".out").c_str(), "w", stdout);
#else
#ifndef ONLINE_JUDGE
    freopen("input.cpp", "r", stdin);
#endif
#endif
     
    int i, t;
    char word[21];
    Trie *root = new Trie();
     
    while (scanf("%d %s", &t, word) > 0) {
        if (t == 0) {
            root->insert_word(word);
        }
        else if (t == 1) {
            root->delete_word(word);
        }
        else if (t == 2) {
            printf("%d\n", root->get_went_here(word));
        }
        else {
            printf("%d\n", root->get_longest_prefix(word, 0));
        }
    }
     
     
    return 0;
}