算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度分析:blablabla
C++ 代码
/**
* // 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) {
int match = 0;
for (int i = 0; i < 10, match < 6; i++) {
int guess_id = rand() % wordlist.size();
match = master.guess(wordlist[guess_id]);
reduceWordList(wordlist, match, wordlist[guess_id]);
}
}
void reduceWordList(vector<string>& wordlist, int match, string cur) {
vector<string> newWordList;
for (int i = 0; i < wordlist.size(); i++) {
if (isMatch(wordlist[i], cur, match)) {
newWordList.push_back(wordlist[i]);
}
}
wordlist = newWordList;
}
bool isMatch(string a, string b, int match) {
int cnt = 0;
if (a.size() != b.size()) {
return false;
}
for (int i = 0; i < a.size(); i++) {
if (a[i] == b[i]) {
cnt++;
}
}
return cnt == match;
}
};