Leetcode 843. Guess the Word
Problem:
This problem is an interactive problem new to the LeetCode platform.
We are given a word list of unique words, each word is 6 letters long, and one word in this list is chosen as secret.
You may call master.guess(word)
to guess a word. The guessed word should have type string
and must be from the original list with 6 lowercase letters.
This function returns an integer
type, representing the number of exact matches (value and position) of your guess to the secret word. Also, if your guess is not in the given wordlist, it will return -1
instead.
For each test case, you have 10 guesses to guess the word. At the end of any number of calls, if you have made 10 or less calls to master.guess
and at least one of these guesses was the secret, you pass the testcase.
Besides the example test case below, there will be 5 additional test cases, each with 100 words in the word list. The letters of each word in those testcases were chosen independently at random from 'a'
to 'z'
, such that every word in the given word lists is unique.
Example 1: Input: secret = "acckzz", wordlist = ["acckzz","ccbazz","eiowzz","abcczz"] Explanation:master.guess("aaaaaa")
returns -1, because"aaaaaa"
is not in wordlist.master.guess("acckzz")
returns 6, because"acckzz"
is secret and has all 6 matches.master.guess("ccbazz")
returns 3, because"ccbazz"
has 3 matches.master.guess("eiowzz")
returns 2, because"eiowzz"
has 2 matches.master.guess("abcczz")
returns 4, because"abcczz"
has 4 matches. We made 5 calls to master.guess and one of them was the secret, so we pass the test case.
Note: Any solutions that attempt to circumvent the judge will result in disqualification.
Solution:
很长的题目,大意是要我们在十次之内在一个wordlist中找到一个预先设定的未知的单词。这道题我的第一想法是先随机挑一个字符串str调用guess()函数得到match,然后在剩下的字符串中寻找和str相同字符数等于match的所有字符串,不断迭代剪枝得到目标字符串secret,错误代码如下:
1 class Solution { 2 public: 3 int getMatch(string &a,string &b){ 4 int count = 0; 5 for(int i = 0;i != a.size();++i){ 6 if(a[i] == b[i]) 7 count++; 8 } 9 return count; 10 } 11 void findSecretWord(vector<string>& wordlist, Master& master) { 12 vector<string> vec(wordlist.begin()+1,wordlist.end()); 13 string str = wordlist[0]; 14 while(true){ 15 vector<string> t; 16 int match = master.guess(str); 17 if(match == 6) return; 18 for(int i = 0;i != vec.size();++i){ 19 if(getMatch(str,vec[i]) == match) 20 t.push_back(vec[i]); 21 } 22 str = t[0]; 23 t.erase(t.begin()); 24 vec = t; 25 } 26 } 27 };
思考下为什么这个算法会失败,直观上来说,我们希望每次剪枝都能删除尽可能多的字符串,而随机挑选字符串的策略并不能满足要求,所以我以第一个字符为目标,在第一个字符出现频率最高的所有字符串中随机挑选一个字符串,然后再进行迭代,这样就可以保证每次都能删掉尽可能多的字符串。不过这道题我还有个疑问,我们是否能够保证在十次之内必定能找到目标字符串,能否用数学方式证明这个结论是否正确。
Code:
1 /** 2 * // This is the Master's API interface. 3 * // You should not implement it, or speculate about its implementation 4 * class Master { 5 * public: 6 * int guess(string word); 7 * }; 8 */ 9 class Solution { 10 public: 11 int getMatch(string &a,string &b){ 12 int count = 0; 13 for(int i = 0;i != a.size();++i){ 14 if(a[i] == b[i]) 15 count++; 16 } 17 return count; 18 } 19 char getMost(vector<string>& words){ 20 vector<int> v(26,0); 21 int maximal = 0; 22 char result; 23 for(string s:words) 24 v[s[0]-'a']++; 25 for(int i = 0;i != 26;++i){ 26 if(v[i] > maximal){ 27 maximal = v[i]; 28 result = 'a'+i; 29 } 30 } 31 return result; 32 } 33 void findSecretWord(vector<string>& wordlist, Master& master) { 34 char c = getMost(wordlist); 35 vector<string> vec(wordlist.begin(),wordlist.end()); 36 string str; 37 for(string s:wordlist){ 38 if(s[0] == c){ 39 str = s; 40 break; 41 } 42 } 43 while(true){ 44 vector<string> t; 45 int match = master.guess(str); 46 if(match == 6) return; 47 for(int i = 0;i != vec.size();++i){ 48 if(getMatch(str,vec[i]) == match && str != vec[i]) 49 t.push_back(vec[i]); 50 } 51 c = getMost(t); 52 for(string s:t){ 53 if(s[0] == c){ 54 str = s; 55 break; 56 } 57 } 58 vec = t; 59 } 60 } 61 };