Cod sursa(job #1582333)

Utilizator japjappedulapPotra Vlad japjappedulap Data 27 ianuarie 2016 20:40:23
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <cstring>
using namespace std;

struct Node{
    bool Ex;
    Node *next[3];
    Node () { Ex = next[0] = next[1] = next[2] = 0; }
}* Root;

char Main[10000031];
char Word[22];

void Input();
void Insert(Node*, char*);
bool Existence(Node*, char*);

int main()
{
    Input();
    int sol = 0;

    for (char *i = Main; *i != '\0'; ++i)
        sol += Existence(Root, i);

    ofstream os ("abc2.out");
    os << sol;
    os.close();
}

void Input()
{
    ifstream is ("abc2.in");
    is >> Main;
    for ( Root = new Node(); is >> Word; Insert(Root, Word));
    is.close();
};

void Insert(Node* x, char* w)
{
    if (*w == '\0')
    {
        x->Ex = 1;
        return;
    }
    if (x->next[*w-'a'] == 0)
        x->next[*w-'a'] = new Node();
    Insert(x->next[*w-'a'], w+1);
};

bool Existence(Node* x, char* w)
{
    if (*w == '\0')
        return x->Ex;

    if(x->Ex == 1)
        return 1;

    if (x->next[*w-'a'] == 0)
        return 0;

    return Existence(x->next[*w-'a'], w+1);
};