Cod sursa(job #1923536)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 11 martie 2017 14:53:42
Problema Trie Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 2.89 kb
#include <cstdio>
#include <cstring>
#include <cctype>
#define maxWordLength 20
using namespace std;

struct node{
    int occurences;
    struct node *edge[26];

    node(){
        occurences = 0;
        for(int i = 0; i < 26; i++){
            edge[i] = NULL;
        }
    }
};
node *root = new node;

void Insert(char word[]){
    node *current = root;
    int len = strlen(word), index;

    for(int i = 0; i < len; i++, current = current -> edge[index]){
        index = tolower(word[i]) - 'a';

        if(current -> edge[index] == NULL){
            current -> edge[index] = new node;
        }
    }current -> occurences++;
}
int Search(char word[]){
    node *current = root;
    int len = strlen(word), index;

    for(int i = 0; i < len; i++, current = current -> edge[index]){
        index = tolower(word[i]) - 'a';
        if(current -> edge[index] == NULL) return 0;
    }
    return current -> occurences;
}
int LongestPrefix(char word[]){
    node *current = root;
    int len = strlen(word), index, prefix = 0;

    for(int i = 0; i < len; i++, current = current -> edge[index]){
        index = tolower(word[i]) - 'a';
        if(current -> edge[index] == NULL) break;
        prefix++;
    }
    return prefix;
}
void Delete(char word[]){
    int len = strlen(word), index, it = -1, sons;
    node *pathStack[maxWordLength + 1], *current = root;

    pathStack[++it] = root;

    for(int i = 0; i < len; i++, current = current -> edge[index]){
        index = tolower(word[i]) - 'a';
        if(current -> edge[index] == NULL) return;
        pathStack[++it] = current -> edge[index];
    }
    if(current -> occurences > 1){
        current -> occurences--;
        return;
    }
    for(int i = it; i > 0; i--){
        sons = 0;

        for(int j = 0; j < 26; j++){
            sons += !!(pathStack[i] -> edge[j]);
        }
        if(sons >= 1) return;
        else{
            delete pathStack[i];
            pathStack[i - 1] -> edge[tolower(word[i - 1]) - 'a'] = NULL;
        }
    }
}

int main(){

    freopen("trie.in", "r", stdin);
    freopen("trie.out", "w", stdout);

    int task;
    char word[21], ch = 0;

    while(1){

        if(isdigit(ch)) task = ch - '0';
        else scanf("%d", &task);
        scanf("%s", word);

        switch(task){
            case 0:{
                Insert(word);
                break;
            }
            case 1:{
                Delete(word);
                break;
            }
            case 2:{
                printf("%d\n", Search(word));
                break;
            }
            case 3:{
                printf("%d\n", LongestPrefix(word));
                break;
            }
        }
        while(1){
            ch = getchar();
            if(isdigit(ch) || ch == EOF) break;
        }if(ch == EOF) break;
    }
    return 0;
}