[LeetCode] 843. Guess the Word 猜单词
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.
这道题号称是 LeetCode 平台上第一个交互式的题目,但定睛一看,踩比赞多,哎,创新不易啊~ 这道题说是有一个单词数组 wordlist,其中有一个单词是需要被猜到的单词 secret,现在有一个 api 函数 guess,可以返回猜的单词和目标单词之间的匹配个数。现在每个 test case 有 10 次机会去猜目标单词,假如调用 api 的次数不超过 10 次,并猜中了目标单词的话,就可以通过测试。首先,由于需要尽可能少的调用 api,所以线性的一个一个的对每个单词调用 api 是不可取的,因为假如目标单词在最后一个,且单词数组长度超过 10 个,就会失败。这样的话可以随机取一个单词来检测,调用 api 后会得到一个次数 cnt,表示当前单词和目标单词之间的匹配个数。接下来怎么办呢?需要过滤一遍单词数组,自己写一个类似于 api 的函数,返回任意两个单词之间的匹配个数,这样就可以 filter 整个单词数组了,因为藏在普通单词中的目标单词跟当前单词调用 match 函数的返回值一定还是 cnt,当然也会有其他的单词同样返回 cnt,不过没关系,还是能滤去一大波不相干的单词,重复这个步骤,直到 cnt 正好为6停止,因为题目中说了单词的长度就是6,参见代码如下:
class Solution {
public:
void findSecretWord(vector<string>& wordlist, Master& master) {
for (int i = 0, cnt = 0; i < 10 && cnt < 6; ++i) {
string guess = wordlist[rand() % (wordlist.size())];
cnt = master.guess(guess);
vector<string> t;
for (string &word : wordlist) {
if (match(guess, word) == cnt) {
t.push_back(word);
}
}
wordlist = t;
}
}
int match(string& a, string& b) {
int res = 0, n = a.size();
for (int i = 0; i < n; ++i) {
if (a[i] == b[i]) ++res;
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/843
参考资料:
https://leetcode.com/problems/guess-the-word/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 843. Guess the Word 猜单词的更多相关文章
- [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- leetcode 843. Guess the Word
我做过的第一个 interactive problem 给一个候选词列表,每次猜测可以猜里面的词,会返回猜中匹配的个数, 可以猜10次, 加上随机化策略之后几乎可以一定通过测试(尽管不是100%) c ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- Leetcode之回溯法专题-79. 单词搜索(Word Search)
Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
- C#版(击败97.76%的提交) - Leetcode 557. 反转字符串中的单词 III - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- python 游戏(猜单词Hangman)
1.游戏思路和流程图 实现功能:随机一个单词让玩家猜测(后续难度实现修改为成语填空,成语必须要有提示,可修改猜的次数,增加连续猜成语,难度系数随着次数的增加而增加) 游戏流程图 2. 单词库和模块 i ...
- LeetCode:前K个高频单词【692】
LeetCode:前K个高频单词[692] 题目描述 给一非空的单词列表,返回前 k 个出现次数最多的单词. 返回的答案应该按单词出现频率由高到低排序.如果不同的单词有相同出现频率,按字母顺序排序. ...
- 关于切片/截取(slice)和random模块的使用(实例:猜单词小游戏)
切片和random的使用在源码中都有注释(可以直接下载):https://github.com/NoobZeng/GuessWords 1. README.MD 基于Python的猜单词游戏 猜单词小 ...
- Python小游戏 -- 猜单词
Python初学者小游戏:猜单词 游戏逻辑:就像我们曾经英语学习机上的小游戏一样,电脑会从事先预置的词库中抽取单词,然后给出单词的字母数量,给定猜解次数,然后让玩家进行猜测,并给出每次猜测的正确字母与 ...
随机推荐
- Sencha Modern
Sencha Modern 前言 经过一个月捣鼓,基于sencha公司最新框架版本 ExtJS7.0 modern 的示例代码可以和大家见面了.示例中对系统主题.公共(组件,方法)封 ...
- 行为驱动:Cucumber + Java - 实现数据的参数化
1.什么是参数化 实际设计测试用例过程中,我们经常会用等价类.边界值这样的方法,针对一个功能进行测试数据上的测试,比如一个输入框,正向数据.逆向数据,非法输入等等 2.Cucumber的数据驱动 同上 ...
- win10每次开机都会自检系统盘(非硬件故障)——解决方案2019.07.12
1.最近反复遇到了这个问题,之前遇到这个问题就把系统重装了,没想到今天又遇到了,目前系统东西太多了,重装太麻烦了,就下决心解决一下. 2.不要使用网络上流传的修改注册表的方案,把注册表的那个键值删除那 ...
- 【07】Nginx:状态统计 / 状态码统计
写在前面的话 在 nginx 中,有些时候我们希望能够知道目前到底有多少个客户端连接到了我们的网站.我们希望有这样一个页面来专门统计显示这些情况.这个需求在 nginx 中是可以实现的,我们可以通过简 ...
- Dev控件使用CheckedListBoxControl获取items.count为0 的解决方法
CheckedListBoxControl,我使用DataSource属性,给其绑定了一个List对象.界面显示都挺正常的,当若干个项的复选框被选中的后,它的checkedListBoxControl ...
- .NET Core RabbitMQ探索(1)
RabbitMQ可以被比作一个邮局,当你向邮局寄一封信时,邮局会保证将这封信送达你写的收件人,而RabbitMQ与邮局最主要的区别是,RabbitMQ并不真的处理信件,它处理的是二进制的数据块,它除了 ...
- DEDECMS安全设置怎样做,您就不会说它不安全了
dedecms是好用,优化也好,就是不安全,个人是这样认为的,今天 闲着没事 整理了一些有助于dedecms网站安全的一些设置,可以说是目前最全的dedecms安全设置! 其一:保持DEDE更新,及时 ...
- Asp.Net MVC强类型页面获取值几种方式
方式一 (V:视图) @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="v ...
- JPA笔记1 ManyToOne
persistence.xml <?xml version="1.0" encoding="UTF-8"?> <persistence ver ...
- postgresql 相关函数总结
1.获取当前日期的年份 select to_char(t.detect_date,'YYYY') select extract(year from now())为double precision 格式 ...