Cod sursa(job #2074046)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 24 noiembrie 2017 00:51:58
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.14 kb
#include <bits/stdc++.h>

using namespace std;

struct trie{
    int cnt = 0, nrfii = 0, here = 0;
    trie *f[30];
    trie(){
        memset(f, 0, sizeof(f));
    }
};

int test, n;
trie *rad = new trie;
char c[25];

void df(trie *t)
{
    for (int i = 0; i < 30; ++i)
        if(t->f[i]){
            cout << char(i+'a') << " " << t->f[i]->cnt << "\n";
            df(t->f[i]);
        }
}

int main()
{
    ifstream fin ("trie.in");
    ofstream fout ("trie.out");
    while(fin >> test){
        fin >> c;
        n = strlen(c);
        trie *t;
        switch(test){
            case 0:
                t = rad;
                for (int i = 0; i < n; ++i){
                    int q = c[i]-'a';
                    if(t->f[q] == NULL){
                        ++t->nrfii;
                        t->f[q] = new trie;
                        ++t->f[q]->cnt;
                    }else ++t->f[q]->cnt;
                    t = t->f[q];
                }
                ++t->here;
                break;
            case 1:
                t = rad;
                for (int i = 0; i < n; ++i){
                    int q = c[i]-'a';
                    if(t->f[q]->cnt == 1){
                        delete t->f[q];
                        t->f[q] = NULL;
                        --t->nrfii;
                        break;
                    }
                    --t->f[q]->cnt;
                    t = t->f[q];
                }
                break;
            case 2:
                t = rad;
                for (int i = 0; i < n; ++i){
                    int q = c[i]-'a';
                    t = t->f[q];
                }
                fout << t->here << "\n";
                break;
            case 3:
                t = rad;
                bool ok = false;
                for (int i = 0; i < n; ++i){
                    int q = c[i]-'a';
                    if(!t->f[q]){
                        ok = true;
                        fout << i << "\n";
                        break;
                    }
                    t = t->f[q];
                }
                if(!ok)
                    fout << n << "\n";
                break;
        }
    }
    return 0;
}