-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadgaddag.cpp
executable file
·143 lines (120 loc) · 3.37 KB
/
loadgaddag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2006 Jason Katz-Brown and John O'Laughlin.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <stdio.h>
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
using namespace std;
unsigned char *dawg;
void spit(int i, string prefix, char counts[29]) {
int index = i * 4;
unsigned int p = (dawg[index] << 16) + (dawg[index + 1] << 8) + (dawg[index + 2]);
char c = dawg[index + 3];
bool t = false;
bool lastchild = false;
if (c & 32) {
t = true;
}
if (c & 64) {
lastchild = true;
}
c = (c & 31) + 'A';
if (c == 27) {
c = 27 + 'A';
}
if ((counts[c - 'A'] >= 1) || (counts[26] >= 1) || (counts[28] >= 1)) {
bool blankhere = false;
bool letterhere = false;
if (counts[c - 'A'] >= 1) {
counts[c - 'A']--;
letterhere = true;
}
else if (counts[26] >= 1) {
counts[26]--;
blankhere = true;
}
if (t) {
bool usedall = true;
for (int j = 0; j < 28; j++) {
if (counts[j] > 0) {
usedall = false;
}
}
if (usedall) {
cout << prefix << c << endl;
}
}
string neUVString = prefix;
if (c <= 'Z') {
neUVString += c;
}
else {
neUVString += '^';
}
if (p != 0) {
spit(p, neUVString, counts);
}
if (letterhere) {
counts[c - 'A']++;
}
else if (blankhere) {
counts[26]++;
}
}
if (!lastchild) {
spit(i + 1, prefix, counts);
}
}
int main() {
dawg = new unsigned char[30000000];
ifstream file("twl.gaddag", ios::in | ios::binary);
int i = 0;
while (!file.eof()) {
file.read((char*)(dawg) + i, 4);
i += 4;
}
while (cin) {
char counts[29];
for (int j = 0; j < 29; j++) {
counts[j] = 0;
}
string query;
cin >> query;
for (int j = 0; j < query.length(); j++) {
char c = query[j];
if (isalpha(c)) {
counts[toupper(c) - 'A']++;
}
else if ((c == '?') || (c == '.')) {
counts[26]++;
}
else if (c == '^') {
counts[27]++;
}
else if ((c == '*') || (c == '/')) {
counts[28]++;
}
}
spit(1, "", counts);
}
}