Cod sursa(job #2082636)

Utilizator hammasattilaHammas Attila hammasattila Data 6 decembrie 2017 17:13:36
Problema Trie Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>

using namespace std;

struct trie {
    int nr = 0;
    trie *kov[26] = { 0 };
} *fa = new trie();

int main()
{
    ifstream f;
    ofstream g;
    f.open("trie.in");
    g.open("trie.out");


    char c[20]; int n;
    while(f >> n >> c) {

        trie *temp_fa = fa;
        int len = strlen(c), tmp;
        if(n == 0) {
            for(int i = 0; i < len; ++i) {
                tmp = c[i] - 'a';
                if(!temp_fa->kov[tmp]) {
                    temp_fa->kov[tmp] = new trie;
                }
                temp_fa = temp_fa -> kov[tmp];
            }
            temp_fa->nr += 1;
        } else if(n == 1) {
            for(int i = 0; i < len; ++i) {
                tmp = c[i] - 'a';
                temp_fa = temp_fa -> kov[tmp];
            }
            temp_fa->nr =- 1;
        } else if(n == 2) {
            for(int i = 0; i < len; ++i) {
                tmp = c[i] - 'a';
                temp_fa = temp_fa -> kov[tmp];
            }
            g << temp_fa->nr << "\n";
        } else if(n == 3) {
            int prefix = 0;
            for(int i = 0; i < len; ++i) {
                tmp = c[i] - 'a';
                if(!temp_fa->nr) { prefix = i; }
                if(!temp_fa -> kov[tmp]) {
                    break;
                }
                temp_fa = temp_fa -> kov[tmp];
            }
            g << prefix << "\n";

        }
    }

    f.close();
    return 0;
}