Cod sursa(job #1221865)

Utilizator somuBanil Ardej somu Data 21 august 2014 16:48:34
Problema Trie Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 2.82 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

#define nmax 26
ifstream in("trie.in");
ofstream out("trie.out");

typedef struct varf {
  
    int prefix;
    int nrAparitii;
    struct varf *desc[26];
    
} VARF, *TRIE;

int tip,Max;
char C[nmax];
TRIE T;
bool ok;

void init(TRIE &T) {
    
    int i;
    T = new VARF;
    for (i=0; i<26; i++)
        T->desc[i] = NULL;
    
}

void adauga(TRIE &T, char C[], int p) {
    
    int ind;
    
    if (T == NULL)
        init(T);
    
    T->prefix = p;
    
    if (C[p] == '\0') {
        T->nrAparitii++;
        return;
    }
    
    ind = int(C[p]-'a');
    
    if (T->desc[ind] != NULL)
        adauga(T->desc[ind], C, p+1);
    else
        init(T->desc[ind]),
        adauga(T->desc[ind], C, p+1);
    
}

void sterge(TRIE &T, char C[], int p) {
    
    int ind,i,cnt;
    
    if (C[p] == '\0') {
        T->nrAparitii--;
        
        if (T->nrAparitii == 0){
            ok = true;
            
            cnt=0;
            
            for (i=0; i<26; i++)
                if (T->desc[i] != NULL)
                    cnt++;
            
            if (cnt == 0) {
                delete(T);
                T = NULL;
            }
        }
        
        return;
    }
    
    ind = int(C[p]-'a');
    
    if (T->desc[ind] != NULL)
        sterge(T->desc[ind], C, p+1);
    
    if (ok) {
        
        cnt=0;
        
        for (i=0; i<26; i++)
            if (T->desc[i] != NULL)
                cnt++;
        
        if (cnt == 0 && T->nrAparitii == 0) {
            delete(T);
            T = NULL;
        }
        
    }
    
}

void aparitii(TRIE &T, char C[], int p) {
    
    int ind;
    
    if (C[p] == '\0'){
        ok = true;
        out << T->nrAparitii << "\n";
        return;
    }
    
    ind = int(C[p]-'a');
    
    if (T->desc[ind] != NULL)
        aparitii(T->desc[ind], C, p+1);
    
}

void lungime(TRIE &T, char C[], int p) {
    
    int ind;
    
    Max = max(Max,  T->prefix);
    
    if (C[p] == '\0') {
        return;
    }
    
    ind = int(C[p]-'a');
    
    if (T->desc[ind] != NULL)
        lungime(T->desc[ind], C, p+1);
    
}

int main() {
    
    init(T);
    
    while (!in.eof()) {
        
        in >> tip >> C;
        
        if (tip == 0) {
            
            adauga(T, C, 0);
            
        } else if (tip == 1) {
            
            ok = false;
            
            sterge(T, C, 0);
            
        } else if (tip == 2) {
            
            ok = false;
            
            aparitii(T, C, 0);
            
            if (!ok)
                out << 0 << "\n";
            
        } else {
            
            Max = 0;
            
            lungime(T, C, 0);
            
            out << Max << "\n";
            
        }
        
    }
    
    return 0;
}