-
Notifications
You must be signed in to change notification settings - Fork 0
/
843. Guess the Word.cpp
40 lines (39 loc) · 1008 Bytes
/
843. Guess the Word.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
/**
* // This is the Master's API interface.
* // You should not implement it, or speculate about its implementation
* class Master {
* public:
* int guess(string word);
* };
*/
class Solution {
public:
void findSecretWord(vector<string>& wordlist, Master& master) {
vector<string>cur, next;
cur = wordlist;
for (int i = 0; i < 10; ++i) {
int n = cur.size();
auto word = cur[rand() % n];
int match = master.guess(word);
if (match == 6) {
break;
}
for (auto& s: cur) {
if (distance(s, word) == match) {
next.push_back(s);
}
}
cur.clear();
swap(cur, next);
}
}
int distance(string& a, string& b) {
int res = 0;
for (int i = 0; i < a.size(); ++i) {
if (a[i] == b[i]) {
++res;
}
}
return res;
}
};