Xinqi Bao's Git
3 * A solution for Wordscapes, an app in appstore game
20 letter() : word(false), next(nullptr){};
26 for (int i
= 0; i
< 26; i
++) ltr
[i
] = new letter
;
29 for (int i
= 0; i
< 26; i
++) {
30 if (ltr
[i
]->next
!= nullptr) delete ltr
[i
]->next
;
35 void insert(const char* w
) {
37 if (w
[0] >= 97 && w
[0] <= 122)
39 else if (w
[0] >= 65 && w
[0] <= 90)
44 ltr
[index
]->word
= true;
46 if (ltr
[index
]->next
== nullptr) ltr
[index
]->next
= new lset
;
47 ltr
[index
]->next
->insert(w
+ 1);
50 bool find(const char* w
) {
52 if (w
[0] >= 97 && w
[0] <= 122)
54 else if (w
[0] >= 65 && w
[0] <= 90)
59 return ltr
[index
]->word
;
60 else if (ltr
[index
]->next
== nullptr)
63 return ltr
[index
]->next
->find(w
+ 1);
66 bool prefix(const char* w
) {
68 if (w
[0] >= 97 && w
[0] <= 122)
70 else if (w
[0] >= 65 && w
[0] <= 90)
74 if (ltr
[index
]->next
== nullptr) return false;
78 return ltr
[index
]->next
->prefix(w
+ 1);
83 void loadDictionary(lset
* dic
, string filename
) {
84 cout
<< "Loading dictionary..." << endl
;
90 cout
<< "Load completed!" << endl
;
100 cout
<< "Find words: blank as '*' || back new scape: 0 \n->";
103 if (str
[0] == '0') return 1;
105 vector
<bool> lstat(letters
.size(), false);
106 for (int i
= 0; i
< str
.length(); i
++) {
111 if (str
[i
] >= 'a' && str
[i
] <= 'z')
113 else if (str
[i
] >= 'A' && str
[i
] <= 'Z')
114 ch
= str
[i
] - 'A' + 'a';
115 for (int k
= 0; k
< letters
.size(); k
++) {
116 if (letters
[k
] == ch
&& lstat
[k
] == false) {
124 int sizeW
= word
.size();
126 finding(word
, lstat
, 0, sizeW
, "", ans
);
127 for (auto& w
: ans
) cout
<< w
<< endl
;
130 void finding(vector
<char> w
, vector
<bool> lst
, int pos
, int ws
, string str
,
133 if (dic
.find(&(str
)[0]))
134 // cout << str << endl;
138 if (pos
!= 0 && !dic
.prefix(&(str
+ '\0')[0])) return;
140 for (int i
= 0; i
< lst
.size(); i
++) {
141 if (lst
[i
] == false) {
142 string st
= str
+ letters
[i
]; // cout <<'-'<<st<<endl;
143 vector
<bool> lst_(lst
);
145 finding(w
, lst_
, pos
+ 1, ws
, st
, ans
);
150 finding(w
, lst
, pos
+ 1, ws
, str
, ans
);
155 scape(lset
& dic
) : dic(dic
) {
156 cout
<< "add letters || back new scape: 0 \n->";
159 if (str
[0] == '0') return;
160 for (int i
= 0; i
< str
.length(); i
++) {
161 if (str
[i
] >= 'a' && str
[i
] <= 'z')
162 letters
.push_back(str
[i
]);
163 else if (str
[i
] >= 'A' && str
[i
] <= 'Z')
164 letters
.push_back(str
[i
] - 'A' + 'a');
166 numLetters
= letters
.size();
167 while (findWords() == 0)
174 loadDictionary(&dic
, "words_alpha.txt");
176 cout
<< "Dictionary loaded!" << endl
;
179 cout
<< "Command: New scape: 1 || exit: 0 \n->";
187 cout
<< "Releasing memory for dictionary..." << endl
;