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 };

原文地址:https://www.cnblogs.com/haoweizh/p/10202516.html

时间: 2024-10-14 23:12:00

Leetcode 843. Guess the Word的相关文章

LeetCode——Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

LeetCode: Length of Last Word [057]

昨天同事在做主从时,从库报如下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set' 粗粗看好像是master的server-id没有设置,但同事做如下查询: 备库采集: [email protected] Fri May 23 14:18:59 2014 14:18:59 [(none)]> show variab

[LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter

843: Guess the Word

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545; min-height: 14.0px } p.p3 { margin: 0.0px 0.0px 2.0px 0.0px;

Leetcode solution 243: Shortest Word Distance

Problem Statement Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. Example: Assume that words = ["practice", "makes", "perfect", "coding", "

LeetCode Length of Last Word

1. 题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-spa

[LeetCode] Length of Last Word 字符串查找

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

leetcode : Length of Last Word [基本功]

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #333333 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #333333; background-color: #ffffff } p.p3 { margin: 0.0px 0.0px 0.0px

leetcode上的pattern word问题

问题: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examples: pattern = "abba", str = "dog cat c