Cod sursa(job #766664)

Utilizator luckyme91wiz kid luckyme91 Data 11 iulie 2012 20:05:51
Problema Trie Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.96 kb
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

struct trie {
    int words;
    int prefixes;
    struct trie* edges[26];
};

ifstream in ("trie.in", ifstream::in);


typedef struct trie Trie;

void init (Trie **vertex) {
    (*vertex) = (Trie*) malloc (sizeof(Trie));
    (*vertex)->words = 0;
    (*vertex)->prefixes = 0;
    for (int i = 0; i < 27; i++) {
        (*vertex)->edges[i] = NULL;
    }
}

void add (Trie* vertex, string word, int position) {
    if (word.length() == position) {
        vertex->words++;
    }
    else 
    {
        vertex->prefixes++;
        int next_pos = word[position] % 26;
        if (vertex->edges[next_pos] == NULL)
        {
            init (&vertex->edges[next_pos]);
        }
        add (vertex->edges[next_pos], word, ++position);
    }
}

void remove (Trie* vertex, string word, int position)
{
    char k;
    if(word.length() != position)
    {
        vertex->prefixes--;
        k = word[position]%26;
        remove (vertex->edges[k], word, ++position);
        if (vertex->edges[k]->prefixes == 0 && vertex->edges[k]->words == 0)
        {
            free(vertex->edges[k]);
            vertex->edges[k] = NULL;
        }
    }
    else
    {
        vertex->words--;
    }
}

void print (Trie* vertex, string word, int position)
{
    if (word.length() == position) 
    {
        printf("%d\n", vertex->words);
    }
    else
    {
        int next_pos = word.at((size_t)position)%26;
        if (vertex->edges[next_pos] != NULL)
        {
            print(vertex->edges[next_pos], word, ++position);
        }
        else
        {
            printf ("0\n");
        }
    }
}

void max_prefix(Trie* vertex, string word, int position)
{
    if (word.length() == position)
    {
        printf("%d\n",position);
    }
    else
    {
        if (vertex->prefixes == 0)
        {
            if (vertex->words == 0)
            {
                printf("%d\n",position - 1);
            }
            else
            {
                printf("%d\n", position);
            }
        }
        else
        {
            int next_pos = word[position] % 26;
            if (vertex->edges[next_pos] != NULL)
            {
                max_prefix (vertex->edges[next_pos], word, ++position);
            }
            else
            {
                printf("%d\n",position);
            }
        }
    }
}


int main() {
    int x;
    string text;
    freopen("trie.out","w",stdout);
    Trie* firstVertex;
    init (&firstVertex);
    in >> x >> text;
    while (in.eof() == false)
    {
       switch (x) {
            case 0 :
                add (firstVertex, text, 0);
                break;
            case 1 :
                remove (firstVertex, text, 0);
                break;
            case 2 :
                print (firstVertex, text, 0);
                break;
            case 3 :
                max_prefix (firstVertex, text, 0);
                break;
        }
        in >> x >> text;
    }
}