-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordracer.cpp
180 lines (152 loc) · 4.16 KB
/
wordracer.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <string>
#include <assert.h>
#define kWordLength 16
#define kBoardBits 4
#define kBoardSize (1 << kBoardBits)
#define TOLOWER( x ) ( (x) >= 'A' && (x) <= 'Z' ? (x) + ('a'-'A') : (x) )
unsigned long stringSignature( const char * str )
{
unsigned long signature = 0;
while( (*str != 0x00 ) ) {
signature = signature | ( 1 << ( TOLOWER(*str) - 'a' ) );
str++;
}
return signature;
}
struct Word
{
Word() : signature( 0 ) {}
void set( const char * word, unsigned int length )
{
memcpy( text, word, length + 1 );
signature = stringSignature( text );
}
unsigned long signature;
unsigned int length;
char text[ kWordLength ];
};
struct Board
{
Board() : signature( 0 ), row( 0 ) {
memset( lengths, 0, sizeof(lengths) );
clearInUse();
}
void clearInUse()
{
memset( inUse, 0, sizeof( inUse ) );
}
void addLine( const char * text )
{
signature |= stringSignature( text );
int i = 0;
while( (*text) != 0x00 ) {
if( (*text) != ' ' ) {
char letter = TOLOWER(*text)-'a';
locations[ letter ][ lengths[ letter ]++ ] = row * kBoardSize + i;
}
++text;
++i;
}
++row;
}
unsigned long signature;
int locations[26][8];
int lengths[26];
bool inUse[kBoardSize*kBoardSize];
int row;
};
bool tryWord( const char * text, int lengthCursor );
Board gBoard;
struct StrLenComparator : public std::binary_function< Word const &, Word const &, bool >{
bool operator()( Word const & LHS, Word const & RHS ) const
{
return LHS.length > RHS.length;
}
};
int main( int argc, char ** argv )
{
std::vector< Word > wordList;
if( argc != 2 ) {
fprintf( stderr, "%s: <wordlist>\n", argv[0] );
exit(1);
}
FILE *fp = fopen( argv[1], "r" );
assert( fp != NULL );
// Generate word list
wordList.reserve(500000);
char text[ kWordLength ];
while( fgets( text, kWordLength, fp ) ) {
// verify first
int textLen = strlen( text );
int i;
text[ --textLen ] = 0x00; //remove endl
for( i = 0 ; i < textLen; ++i ) {
if( !( ( text[i] >= 'A' && text[i] <= 'Z' ) || ( text[i] >= 'a' && text[i] <= 'z' ) ) ) {
break;
}
}
if( i == textLen && textLen > 2 ) {
wordList.push_back( Word() );
wordList.back().set( text, textLen );
}
}
fclose( fp );
std::sort( wordList.begin(), wordList.end(), StrLenComparator() );
// Read board
printf("Enter board\n");
while( fgets( text, kWordLength, stdin ) ) {
int textLen = strlen( text );
if( textLen == 1 ) {
break;
}
text[ --textLen ] = 0x00; //remove endl
gBoard.addLine( text );
}
// Now solve
std::vector< Word >::iterator begin = wordList.begin();
std::vector< Word >::iterator end = wordList.end();
std::vector< Word >::iterator i;
for( i = begin; i != end; ++i ) {
if( ( (*i).signature | gBoard.signature ) == gBoard.signature ) {
// Check this word more
char * text = (*i).text;
//printf("%s\n", text );
if( tryWord( text, 0 ) ) {
printf("%s\n", text );
gBoard.clearInUse();
}
}
}
}
bool tryWord( const char * text, int lengthCursor )
{
char letterIdx = TOLOWER(*text) - 'a';
if( *(++text) == 0x00 ) {
return true;
}
if( gBoard.lengths[ letterIdx ] == lengthCursor ) {
return false;
}
char nextLetterIdx = TOLOWER(*text) - 'a';
if( letterIdx == 'q' - 'a' && nextLetterIdx == 'u' - 'a' ) {
return tryWord( ++text, 0 ); // qu is always allowed
}
int loc = gBoard.locations[ letterIdx ][ lengthCursor ];
gBoard.inUse[ loc ] = true;
for( int a=0; a < gBoard.lengths[ nextLetterIdx ]; ++a ) {
int nextLoc = gBoard.locations[ nextLetterIdx ][ a ];
if( nextLoc == loc + kBoardSize || nextLoc == loc - kBoardSize || nextLoc == loc + 1 || nextLoc == loc - 1 ||
nextLoc == loc + kBoardSize - 1 || nextLoc == loc + kBoardSize + 1 ||
nextLoc == loc - kBoardSize - 1 || nextLoc == loc - kBoardSize + 1 ) {
if( gBoard.inUse[ nextLoc ] == false && tryWord( text, a ) ) {
return true;
}
}
}
gBoard.inUse[ loc ] = false;
return false;
}