Leetcode#843 Guess the Word

题目大意:

  • 这是一个Leetcode平台新型的交互式问题

我们会给定一个不重复的词表,里面都是只有6个小写字母的单词。然后,系统会随机选定一个单词作为 "secret"

你可以调用系统提供的API master.guess(word) 来查询word 是否就是 "secret"。
这个API返回的参数是个整型,表示查询的匹配程度。

对于每个测试用例,你有10次查询机会。如果你能在10次以内的查询中找出 "secret" 则判定你通过用例。

样例输入:

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.

  • 词表长度为 100

解题思路:

题目乍一看,特别像小时候 非常6+1节目,主持人让观众查价格,也有反馈机制,会告诉距离目标价格高了 还是 低了。

给出的wordlist词表长度,一定会大于10。不然遍历枚举查询就可以了(┑( ̄Д  ̄)┍)

基于贪心策略,我们应该要利用好系统的反馈机制,避免我们浪费查询机会。我们的目标应该是提高每次的 matches 值,即每次都要淘汰词表里不等于matches的单词,缩小我们枚举范围。

每次查询之后,如果matches不等于6,我们虽然还不知道"secret"。但我们知道哪些一定不是"secret",进而缩小范围,逼近答案。

/**
6 / 6 test cases passed.
Status: Accepted
Runtime: 2 ms
*/

class Master {
  public:
    int guess(string word);
};


class Solution {
public:
    int match(const string a, const string b){
        int ans = 0;
        
        for(int i = 0;i<a.length(); i++){
            if(a[i] == b[i]) ++ans;
        }
        
        return ans;
    }
    
    void shrinkWordList(vector<string>& wordlits, const string guessWord, const int matches){
        vector<string> tmp;
        
        
        for(string word : wordlits){
            int m = match(word, guessWord);
            if(m == matches){
                tmp.push_back(word);
            }
        }
        
        wordlits = tmp;
    }
    
    void findSecretWord(vector<string>& wordlist, Master& master) {
        
        string target = wordlist[random() % wordlist.size()];
        int Count = 10;
        while(Count--){
            int matches = master.guess(target);
            
            shrinkWordList(wordlist, target, matches);
            
            target = wordlist[random() % wordlist.size()];
        }
        
    }
};


后记

注意到

Note: Any solutions that attempt to circumvent the judge will result in disqualification.

任何绕过判定的做法都会被视为 非法。

比如,我们把Count 初始值设置为 10000,提交一下:

Output:
Either you took too many guesses, or you did not find the secret word.

:)猜测每次TC,系统会限制master.guess调用次数。内部控制了10次,超过10次查询没有得到matches=6,就判定不通过用例。

这里还有一个问题,为什么100个不重复且长度都为6的单词,可以通过10次查询一定会猜中 "secret"。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容