leetCoder-wordBreak判断能否分词
来源:互联网 发布:下载wps0ff1ce软件 编辑:程序博客网 时间:2024/05/18 01:48
题目
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
For example, given
s = “leetcode”,
dict = [“leet”, “code”].
Return true because “leetcode” can be segmented as “leet code”.
UPDATE (2017/1/4):
The wordDict parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
分析
给出一段话,判断能否分词(被dict里的单词分割)
简单动态规划题,使用dp[i]
表示前i
个单词能否被分词,则状态转移方程为
dp[j] = dp[i] && s[i:j]∈dict
如果前i
个单词可以被分词,且i-j
在dict
里,则前j
个单词可以被分词
AC代码
class Solution {public: bool wordBreak(string s, vector<string>& wordDict) { int n = s.length(); vector<bool> dp(n + 1, false); dp[0] = true; for(int i=0;i<n;i++){ for(int j = i; dp[i]&&j<n; j++){ auto f = find(wordDict.begin(), wordDict.end(), s.substr(i,j-i+1)); if(f != wordDict.end())//找到 dp[j+1] = true; } } return dp[n]; }};
阅读全文
0 0
- leetCoder-wordBreak判断能否分词
- wordbreak
- WordBreak
- WordBreak
- leetcode wordBreak
- leetcode12.wordBreak
- leetcode wordBreak
- Leetcode - WordBreak
- leetcode wordbreak
- Leetcode: wordBreak
- leetcode-wordBreak
- leetcode-wordbreak
- wordBreak-ii
- WordBreak解决
- 大数除法 判断能否整除
- js 判断客户端能否上网
- 判断NSImage能否显示出来
- shell假设判断能否上网
- dp、dip、sp、pt和px的区别和转换
- 第四周
- 交互设计师判断该不该接需求的五个小原则
- 如何进行多个按钮之间来进行切换
- test
- leetCoder-wordBreak判断能否分词
- create-react-app my-appcreate-react-app慢的解决方法
- P1125 笨小猴
- 个性
- ioc容器源码分析(一)
- React diff
- Java回调
- MariaDB字符集编码
- HashMap源码解析