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 ...
随机推荐
- Erlang节点重启导致的incarnation问题(转)
转自霸爷的博客: 转载自系统技术非业余研究 本文链接地址: Erlang节点重启导致的incarnation问题 遇到个问题, =ERROR REPORT==== 10-Mar-2016::09:44 ...
- linux 上操作常用的命苦与出错的地方
帮助信息 ./configure -help|grep mysql 出错提示安装libxml2 tar -zxvf libxml2xxxx.tar cd libxml2xxx ./configure ...
- 如何给UIViewController瘦身
本文转载至 http://www.cocoachina.com/ios/20141128/10356.html 随着程序逻辑复杂度的提高,你是否也发现了App中一些ViewController的代码 ...
- delphi 颜色 引用http://www.cnblogs.com/del/archive/2008/02/19/1073568.html
颜色名称 颜色效果 Hex HTML clBlack $000000 #000000 clMaroon $000080 #800000 clGreen $008000 #00800 ...
- POJ2594 Treasure Exploration[DAG的最小可相交路径覆盖]
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8301 Accepted: 3 ...
- sql server单引号和双引号的区别
--当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔--当 SET QUOTED_IDENTIFIER 为 OFF(默认值)时,表达式中的文 ...
- iOS 流布局 UICollectionView使用(UICollectionVIew的代理方法)
UICollectionViewDataSource协议 这个协议主要用于collectionView相关数据的处理,包含方法如下: 设置分区数(这个是可选实现的) - (NSInteger)numb ...
- 2017-2018-1 20179209《Linux内核原理与分析》第二周作业
本周课业主要通过分析汇编代码执行情况掌握栈的变化.本人本科时期学过intel 80X86汇编语言,所以有一定基础:在Linux中32位AT&T风格的汇编稍微熟悉就可以明白.所以我学习的重点放在 ...
- 剑指Offer:删除链表的节点【18】
剑指Offer:删除链表的节点[18] 题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3-& ...
- 基于ajax的登录
验证码 当登录一个网站的时候往往会有验证码. python生成随机验证码,需要使用到 PIL 模块 安装 : pip3 install pillow 1. 创建图片 我们现在写的验证码属 ...