Cod sursa(job #1523013)

Utilizator meriniucrMeriniuc Razvan- Dumitru meriniucr Data 12 noiembrie 2015 12:47:06
Problema Trie Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 2.75 kb
#include <fstream>
#include <string.h>
#include <string>
 
using namespace std;
 
ifstream mama("trie.in");
ofstream tata("trie.out");
 
struct node
{
    string  word;
    int     appearances;
    node    *suffixes[26];
    node()
    {
        word = "";
        appearances = 0;
        memset(suffixes, 0, sizeof(suffixes));
    }
};
 
node *base;
 
void    insert(string w)
{
    int     index;
    int     length;
	node    *new_node;
    
	index = 0;
    length = w.length();
    while (index < length and base -> suffixes[w[index] - 'a'])
    {
        base = base -> suffixes[w[index] - 'a'];
        index += 1;
    }
    while (index < length)
    {
        new_node = new node;
        new_node -> word = base -> word + w[index];
        base -> suffixes[w[index] - 'a'] = new_node;
        base = base -> suffixes[w[index] - 'a'];
        index += 1;
    }
    base -> appearances += 1;
}
 
void    del(string w)
{
    int     index;
    int     length;
    node    *t;
    node    *p;
    int     ind;
 
    index = 0;
    length = w.length();
    t = base;
    p = base;
    ind = 0;
    while (index < length and base -> suffixes[w[index] - 'a'])
    {
        int i;
        base = base -> suffixes[w[index] - 'a'];
        for (i = 0; i < 26 and (!base -> suffixes[i] or i == w[index] - 'a'); i += 1);
        if (i != 26)
        {
            p = base;
            ind = index;
        }
        index += 1;
    }
    if (!(base -> appearances -= 1))
        p -> suffixes[w[ind + 1] - 'a'] = 0;
}
 
void    appear(string w)
{
    int     index;
    int     length;
 
    index = 0;
    length = w.length();
    while (index < length and base -> suffixes[w[index] - 'a'])
    {
        base = base -> suffixes[w[index] - 'a'];
        index += 1;
    }
    if (length == index)
        tata << base -> appearances << '\n';
    else
        tata << 0 << '\n';
}
 
void    prefix(string w)
{
    int counter;
    int index;
    int length;
    int i;
 
    length = w.length();
    index = 0;
    counter = 0;
    while (index < length)
    {
        if (!base -> suffixes[w[index] - 'a'])
        {
            tata << counter << '\n';
            return;
        }
        base = base -> suffixes[w[index] - 'a'];
        index += 1;
        counter += 1;
    }
    tata << counter << '\n';
}
 
int     main()
{
    string input;
    char op;
    string word;
    node *temp;
 
    base = new node;
    while (getline(mama, input))
    {
        temp = base;
        op = input[0];
        word.assign(input.begin() + 2, input.end());
        if (op == '0')
            insert(word);
        else if (op == '1')
            del(word);
        else if (op == '2')
            appear(word);
        else if (op == '3')
            prefix(word);
        base = temp;
    }
         
    return 0;
}