Pagini recente » Cod sursa (job #1713258) | Cod sursa (job #734791) | Cod sursa (job #1365993) | Cod sursa (job #2404855) | Cod sursa (job #1474163)
#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];
}