leetcode Word Break-单词划分
- 题目描述:
- 给定一个字符串s和一组单词,确定这个字符串是否能够进行这样一种划分,划分后所有的子字符串均来自给定的单词组。例如s = “leetcode” ,dict = {“leet”,“code”},那么s可以由dict中的单词组成。
- 题目来源:
- http://oj.leetcode.com/problems/word-break/
- 题目分析:
- 可以将原问题的解通过解子问题来解决,用dp[i]表示字符串s从1到i是否能够进行这样的划分,假如s[1...j - 1](j - 1 < i)能够进行这样的划分并且s[j...i]是给定字典中的某个单词,那么,容易知道s[1...i]也能够进行这样的划分,计算位置 i 时,查找所有符合条件的单词,然后通过判断子问题的解来确定当前的解。通过迭代计算得到最终解。在进行字符串匹配的时候通过Trie结构优化。使用Trie结构找到所有符合条件的单词用线性时间。
- 时间复杂度:O(n^2 + t*l)(n为字符串s的长度,t为字典中单词的个数,l为字典单词的平均长度)
- 示例代码:
struct tree_node {
bool isstr;
int next[branchNum];
}node[Max];
bool wordBreak(string s, unordered_set<string> &dict) {
p = ;
memset(node[].next, , sizeof(node[].next));
int n = s.length();
unordered_set<string>::iterator it = dict.begin();
while(it != dict.end()) {
insertstr(*it);
++it;
}
vector<bool> dp(n + , false);
int location = ;
for(int i = n - ; i >= ; --i) {
location = ;
for(int j = i; j < n; ++j) {
if(node[location].next[s[j] - 'a'] == )
break;
location = node[location].next[s[j] - 'a'];
if(node[location].isstr) {
if(j == n - )
dp[i] = true;
else {
dp[i] = dp[i] | dp[j + ];
if(dp[i])
break;
}
}
}
}
return dp[];
}
void insertstr(string t) {
const char *word = t.c_str();
int location = ;
while(*word) {
if(node[location].next[*word - 'a'] == || node[location].next[*word - 'a'] >= p) {
node[p].isstr = false;
memset(node[p].next, , sizeof(node[p].next));
node[location].next[*word - 'a'] = p++;
}
location = node[location].next[*word - 'a'];
word++;
}
node[location].isstr = true;
}
leetcode Word Break-单词划分的更多相关文章
- [LeetCode] 139. Word Break 单词拆分
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode:Word Break II(DP)
题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...
- LeetCode ||& Word Break && Word Break II(转)——动态规划
一. Given a string s and a dictionary of words dict, determine if s can be segmented into a space-sep ...
- LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [LeetCode] Word Break II 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [leetcode]Word Break II @ Python
原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words ...
- [Leetcode] word break ii拆分词语
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- LeetCode 139. Word Break单词拆分 (C++)
题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...
随机推荐
- ASP.NET动态网站制作(9)-- JQ(1)
前言:从这节课开始讲jQuery的相关内容,这节课主要围绕jQuery的选择器展开. 内容: 1.jQuery是一个优秀的js框架,目前企业里大多数都是用jQuery(以下简称jq).jq是对js里一 ...
- phpstorm pycharm IntelliJ IDEA激活
phpstorm 和 pycharm server选项里边输入 http://idea.imsxm.com/ IntelliJ IDEA 1. 到网站 http://idea.lanyus.com/ ...
- python 基础 8.1 r 正则对象
...
- Awesome Vue.js vue.js学习资源链接大全 中文
https://blog.csdn.net/caijunfen/article/details/78216868
- 【BZOJ1018】[SHOI2008]堵塞的交通traffic 线段树
[BZOJ1018][SHOI2008]堵塞的交通traffic Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个 ...
- Centos7升级python版本
升级Python 我的安装目录是/home/python 下载 ` cd /home/python wget https://www.python.org/ftp/python/3.5.0/Pytho ...
- BZOJ4920: [Lydsy1706月赛]薄饼切割
BZOJ4920: [Lydsy1706月赛]薄饼切割 Description 有一天,tangjz送给了quailty一张薄饼,tangjz将它放在了水平桌面上,从上面看下去,薄饼形成了一个H*W的 ...
- PHP通过session id 实现session共享和登录验证的代码
先说说,这个机制的用途吧,到现在为止战地知道这个机制有两个方面的用途: 首先,多服务器共享session问题,这个大家应该都能够理解的,当一个网站的用户量过大,就会使用服务器集群,例如专门有一个登录用 ...
- MySQL——并发控制(锁)
核心知识点: 1.表锁和行级锁代表着锁的级别:读锁和写锁代表锁定真实类型. 2.读锁属于共享锁,共享同一资源,互不干扰:写锁属于排他锁,为了安全起见,写锁会阻塞其他的读锁和写锁. 3.表锁的开销最小, ...
- Java for LeetCode 101 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...