Cod sursa(job #1474163)

Utilizator sabin.antoheSabin Antohe sabin.antohe Data 21 august 2015 10:43:15
Problema Secventa Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <iostream>
#include <string>
#include <fstream>
#include <stack>
#include <map>
#define ALPHABET_SIZE 26

class Trie {
private:
    struct node {
        bool isEnd;
        node *child[ALPHABET_SIZE];
    } *root, *runner;
    std::stack<node*> backtrack;
    std::map<int, int> saves;

public:
    Trie(void) {
        root = new node();
        root -> isEnd = false;
        runner = root;
    }
    ~Trie(void) {purge(root);}

    void purge(node *current) {
        for(int i = 0; i < ALPHABET_SIZE; i++)
            if(current -> child[i] != NULL)
                purge(current -> child[i]);

        delete current;
    }

    void insert(std::string word) {
        node *current = root;
        for(int i = 0; i < word.length(); i++) {
            const int letter = (int)word[i] - (int)'a';
            if(current -> child[letter] == NULL)
                current -> child[letter] = new node();
            current = current -> child[letter];
        }
        current -> isEnd = true;
    }

    bool find(char letter) {
        if(runner -> child[(int)letter - (int)'a'] != NULL) {
            backtrack.push(runner);
            runner = runner -> child[(int)letter - (int)'a'];
            return true;
        }
        else return false;
    }
};


int main(void) {
    std::ifstream file("words.txt");
    std::string line;
    Trie words;

    if(file.is_open()) {
        while(getline(file, line))
            words.insert(line);
        file.close();
    }
    else return 1; //error

    file.open("grid.txt");
    getline(file, line);
    const int M = line.length();
    file.seekg(0, file.end);
    const int N = file.tellg() / line.length();
    file.seekg(0, file.beg);

    char grid[N][M];
    for(int i = 0; i < N; i++)
        for(int j = 0; j < M; j++)
            file >> grid[i][j];

}