-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.cpp
319 lines (273 loc) · 6.44 KB
/
anagram.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Anagram.cpp
// written by M. Hoskins "Hashish Innocently Amok" ([email protected])
// last revision: 2013/12/29
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct wlelm { // world list element
std::string word;
wlelm *next;
wlelm *prev;
};
struct wlist { // word list
wlelm *head;
wlelm *tail;
};
int numberOf=-1, numberAnagram=0;
int myfind(std::string &searchst, char letter) { // find position of char 'letter' in string 'searchst'
unsigned int i;
for (i=0; i<searchst.length(); i++)
if ( searchst[i]==letter )
return i;
return -1;
}
char tolower(char c) { // if char 'c' is uppercase, change it to lowercase
if (c>='A' && c<='Z')
return (char) c-'A'+'a';
else
return c;
}
void downcase(std::string &str) // converrt string 'str' to all lowercase
{
const int length = str.length();
for(int i=0; i < length; ++i)
{
str[i] = tolower(str[i]);
}
}
// The following six functions are for working with word lists
void wlappend(wlist &list, std::string x) {
if (list.head==NULL) {
list.head=list.tail=new wlelm;
list.head->prev=NULL;
list.head->next=NULL;
}
else {
list.tail->next=new wlelm;
list.tail->next->prev=list.tail;
list.tail->next->next=NULL;
list.tail=list.tail->next;
}
list.tail->word=x;
}
void wlerase(wlist &l) {
wlelm *temp;
while(l.tail!=NULL) {
temp=l.tail;
l.tail=l.tail->prev;
delete temp;
}
l.head=NULL;
}
bool wlempty(wlist l) { return l.head==NULL; }
void wlrecsort(wlist &l, wlelm *current) {
wlelm *sorting,*find,*nextsort=NULL;
bool curtail, changelist=false;
if (current==NULL)
return;
if (l.head==l.tail)
return;
if (current==l.head)
current=current->next;
curtail=(current==l.tail);
if (current->word.length()>current->prev->word.length()) {
changelist=true;
nextsort=current->next;
sorting=current;
sorting->prev->next=sorting->next;
if (!curtail)
sorting->next->prev=sorting->prev;
else
l.tail=sorting->prev;
find=sorting->prev;
while( ((find==l.head) ? false : find->prev->word.length()<sorting->word.length()))
find=find->prev;
if (find==l.head) {
l.head->prev=sorting;
sorting->next=l.head;
l.head=sorting;
}
else {
find->prev->next=sorting;
sorting->prev=find->prev;
sorting->next=find;
find->prev=sorting;
}
if (curtail)
return;
}
if (changelist)
current=nextsort;
else
current=current->next;
wlrecsort(l, current);
}
void wlsort (wlist &l) {
wlelm *current;
if (wlempty(l))
return;
current=l.head->next;
wlrecsort(l, current);
}
void wlprint(wlist l) {
wlelm *current;
int num=0;
current=l.head;
cout << "BEGIN wl---";
cout << "Word List--------" ;
while(current!=NULL) {
cout << current->word << " ";
num++;
current=current->next;
}
cout << num << "---END wl" << endl;
}
void remove(std::string &x, int startpos, int count) {
std::string n="";
int i,l;
l=x.length();
if (startpos>=0 && startpos+count<=l) {
for(i=0;i<startpos;i++)
n+=x[i];
for(i=startpos+count; i<l; i++)
n+=x[i];
x=n;
}
}
bool issubstr(std::string word, std::string phrase, std::string &left) { // boolean result of "Is 'word' found in 'phrase'?"
int pos;
unsigned int lenw;
std::string temp,letter,nleft;
char let;
lenw=word.length();
if (lenw>phrase.length())
return false;
temp=word;
left=phrase;
while (lenw--) {
let=temp[0];
pos=myfind(left, let);
if (pos<0)
return false;
remove(temp,0,1);
remove(left,pos,1);
}
return true;
}
void allchar(std::string &x) { // remove anything but lowercase letters in string 'x'
std::string n="";
int lenw=x.length(),i;
for (i=0; i<lenw; i++)
if (x[i]>='a' && x[i]<='z')
n+=x[i];
x=n;
}
void anagram(std::string bstr, wlist list, std::string anstr) { // main recursive workhorse function
wlelm *curword, *ncword;
std::string newanstr, left;
wlist nlist;
nlist.head=NULL;
nlist.tail=NULL;
curword=list.head;
while(curword!=NULL) {
issubstr(curword->word,anstr,newanstr);
if (newanstr=="") {
cout << bstr << curword->word << endl;
if (++numberAnagram==numberOf)
exit(0);
}
else {
if (issubstr(curword->word, newanstr, left))
ncword=curword;
else
ncword=curword->next;
while(ncword!=NULL) {
if (issubstr(ncword->word, newanstr, left)) {
if (left=="") {
cout << bstr+curword->word+" " << ncword->word << endl;
if (++numberAnagram==numberOf)
exit(0);
}
else
wlappend(nlist, ncword->word);
}
ncword=ncword->next;
}
if (nlist.head!=NULL) {
anagram(bstr+curword->word+" ",nlist, newanstr);
wlerase(nlist);
}
}
curword=curword->next;
}
}
int main(int argc, char *argv[]) {
ifstream dict;
std::string dictword, anstr, temp, left, required, throwaway;
std::string exename, comarg1, comarg2, comarg3;
wlist list;
char * pEnd;
exename=argv[0];
if (argc>1) {
comarg1=argv[1];
anstr=comarg1;
}
else {
cerr << exename << ": Must supply string to be anagrammed. Exiting..." << endl;
return -1;
}
if (argc>2) {
comarg2=argv[2];
required=comarg2;
if (required=="-")
required="";
}
else
required="";
if (argc>3) {
comarg3=argv[3];
numberOf=strtol(argv[3], &pEnd, 10);
if (numberOf<1) {
cerr << exename << ": error, third argument must be a positive integer. Exiting..." << endl;
return -1;
}
}
list.head=NULL;
list.tail=NULL;
dict.open("/usr/dict/anagramwords"); // A list of english words, one per line
if (dict.fail()) {
cerr << exename << ": error: cant open /usr/dict/anagramwords. Exiting..." << endl;
exit(-1);
}
downcase(anstr);
downcase(required);
allchar(anstr);
allchar(required);
if (required.length()>0) {
if (issubstr(required,anstr,temp))
anstr=temp;
else {
cerr << exename << ": error: '" << required << "' isn't in the string to be anagrammed. Exiting...." << endl;
exit(-1);
}
}
dict >> dictword;
allchar(dictword);
while (!dict.eof()) {
downcase(dictword);
if (issubstr(dictword, anstr, left))
wlappend(list, dictword);
dict >> dictword;
allchar(dictword);
}
dict.close();
wlsort(list);
// wlprint(list);
if (required!="")
anagram(required+" ", list, anstr);
else
anagram("", list, anstr);
exit(0);
}