Cod sursa(job #1798576)

Utilizator ajeccAjechiloae Eugen ajecc Data 5 noiembrie 2016 12:09:43
Problema Trie Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.45 kb
#include <bits/stdc++.h>
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A, x) memset(A, x, sizeof(A))
#define cpy(A, B) memcpy(A, B, sizeof(B))
#define g(s) getline(cin, s) ///ai grija la fin/cin ///
#define FASTIO ios_base::sync_with_stdio(0)
const long long INFLL = (1LL<<62);
const int INFINT = 2000000000;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
/*template <typename T>
string to_string(const T& n){
    ostringstream os;
    os << n;
    return os.str();
}*/

/*void invers_modular(int a, int b, int &d, int &x, int &y)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
        return ;
    }
    int x0, y0;
    invers_modular(b, a%b, d, x0, y0);
    x=y0;
    y=x0-a/b*y0;
}*/ // daca x<0 se aduna cu mod pana e mai mare, x fiind rezultatul

/*ull putere(ull baza, ull exponent, ull MOD)
{
    if(exponent == 0) return 1;
    if(exponent % 2 == 0) return putere((baza * baza) % MOD, exponent / 2, MOD) % MOD;
    return ((baza % MOD) * (putere(baza, exponent - 1, MOD) % MOD) % MOD);
}*/
ifstream fin("trie.in"); /// modifica cu numele corespunzator
ofstream fout("trie.out"); /// modifica cu numele corespunzator

struct trie
{
    trie *nod[26];
    int cnt;

    trie()
    {
        cnt = 0;
        for0(i, 26)
            nod[i] = NULL;
    }
};
trie *root = new trie;

int op;
string s;

void adaugare()
{
    trie *aux = root;
    for(auto i: s)
    {
        if(aux -> nod[(int) i - 'a'] == NULL)
        {
            aux -> nod[(int) i - 'a'] = new trie;
            aux -> nod[(int) i - 'a'] -> cnt += 1;
            aux = aux -> nod[(int) i - 'a'];
            continue;
        }
        aux -> nod[(int) i - 'a'] -> cnt += 1;
        aux = aux -> nod[(int) i - 'a'];
    }
}

void stergere()
{
   trie *aux = root;

   for0(i, s.size())
   {
        aux -> nod[(int) s[i] - 'a'] -> cnt = max(aux -> nod[(int) s[i] - 'a'] -> cnt - 1, 0);
        aux = aux -> nod[(int) s[i] - 'a'];
   }
}

void nr_aparitii()
{
    trie *aux = root;
    int sol = 0;
    for0(i, s.size())
    {
        if(aux -> nod[(int) s[i] - 'a'] != NULL)
        {
            sol = aux -> nod[(int) s[i] - 'a'] -> cnt;
            aux = aux -> nod[(int) s[i] - 'a'];
        }
        else
        {
            fout << "0\n";
            return ;
        }
    }
    int sum = 0;

    for0(i, 26)
        if(aux -> nod[i] != NULL)
            sum += aux -> nod[i] -> cnt;
    fout << sol - sum << '\n';
}

void prefix()
{
    trie *aux = root;
    int sol = 0;
    for(auto i: s)
        if(aux -> nod[(int) i - 'a'] != NULL && aux -> nod[(int) i - 'a'] -> cnt)
        {
           // cout << i << ' ';
            sol++;
            aux = aux -> nod[(int) i - 'a'];
        }
        else break;
   // cout << endl;
    fout << sol << '\n';
}

int main()
{
    while(fin >> op >> s)
    {
        switch(op)
        {
        case 0:
            adaugare();
            break;
        case 1:
            stergere();
            break;
        case 2:
            nr_aparitii();
            break;
        case 3:
            prefix();
            break;
        default:
            cout << "EROARE";
        }
    }

    return 0;
}