Cod sursa(job #1863800)

Utilizator 1475369147896537415369Andrei Udriste 1475369147896537415369 Data 31 ianuarie 2017 10:56:00
Problema Trie Scor 60
Compilator c Status done
Runda Arhiva educationala Marime 3.87 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <ctype.h>

struct node{
    int occurences;
    struct node *next[26];
};
struct node *head = NULL;

void Initialize(){
    head = (struct node*)malloc(sizeof(struct node));
    head -> occurences = 0; int i;

    for(i=0; i<26; i++){
        head -> next[i] = NULL;
    }
}
void Insert(char word[]){
    struct node *ptr = head;
    int i, j, len = strlen(word), index;

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

        if(ptr -> next[index] == NULL){
            ptr -> next[index] = (struct node*)malloc(sizeof(struct node));
            (ptr -> next[index]) -> occurences = 0;

            for(j=0; j<26; j++){
                (ptr -> next[index]) -> next[j] = NULL;
            }
        }
    }ptr -> occurences++;
}
int Search(char word[]){
    struct node *ptr = head;
    int i, len = strlen(word), index;

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

    for(i=0; i<len; i++, ptr = ptr -> next[index]){
        index = tolower(word[i] - 'a');
        if(ptr -> next[index] == NULL) return prefix;
        int vertices = 0;

        for(j=0; j<26; j++){
            vertices += !!((ptr -> next[index]) -> next[j]);
        }
        if((ptr -> next[index]) -> occurences || vertices >= 1){
            prefix = i+1;
        }
    }
    return prefix;
}
void Delete(char word[21]){
    struct node *ptr = head, *depth = NULL;
    int i, j=-1, k, index, len = strlen(word), vertices = 0;

    for(i=0; i<len; i++, ptr = ptr -> next[index]){
        index = tolower(word[i] - 'a');
        if(ptr -> next[index] == NULL) return;
        if(i != len-1 && ptr -> occurences){
            depth = ptr;
            j = i;
        }
        vertices = 0;

        for(k = 0; i && k < 26; k++){
            vertices += !!(ptr -> next[k]);
        }if(vertices > 1 && i > j){
            j = i;
            depth = ptr;
        }

    }if(ptr -> occurences == 0) return;

    vertices = 0;

    for(i=0; i<26; i++){
        vertices += !!(ptr -> next[i]);
    }
    if(ptr -> occurences > 1 || ((ptr -> occurences == 1) && vertices)){
        ptr -> occurences--;
        return;
    }
    if(depth == NULL){
        index = tolower(word[0] - 'a');
        ptr = head -> next[index];
        head -> next[index] = NULL;

        for(i=1; i<len; i++){
            index = tolower(word[i] - 'a');
            struct node *temp = ptr;
            ptr = ptr -> next[index];
            free(temp);
        }free(ptr);
    }else{
        k = len - j;
        index = tolower(word[j] - 'a');
        struct node *temp = depth;
        depth = depth -> next[index];
        temp -> next[index] = NULL;

        for(i=1; i<k; i++){
            index = tolower(word[j+i] - 'a');
            temp = depth;
            depth = depth -> next[index];
            free(temp);
        }free(depth);
    }
}

int main(){

Initialize();

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

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

while(1){
    if(isdigit(ch)){
        task = ch - 48;
    }else{
        scanf("%d", &task);
    }
    scanf("%s", word);

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