题目描述:
给定一个字符串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-单词划分的更多相关文章

  1. [LeetCode] 139. Word Break 单词拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  2. 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 ...

  3. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  4. LeetCode:Word Break II(DP)

    题目地址:请戳我 这一题在leetcode前面一道题word break 的基础上用数组保存前驱路径,然后在前驱路径上用DFS可以构造所有解.但是要注意的是动态规划中要去掉前一道题的一些约束条件(具体 ...

  5. 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 ...

  6. LeetCode Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words  ...

  7. [LeetCode] Word Break II 解题思路

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  8. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  9. [Leetcode] word break ii拆分词语

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  10. LeetCode 139. Word Break单词拆分 (C++)

    题目: Given a non-empty string s and a dictionary wordDict containing a list of non-emptywords, determ ...

随机推荐

  1. 多媒体开发之---h264 高度和宽度获取

    ( School of Computer Science & Technology, Soochow University,SuZhou 215006:) Abstract: H.264 is ...

  2. ios -- 延迟3秒触发performSelector

    [self performSelector:@selector(changeText:) withObject:@"Happy aha" afterDelay:3];

  3. RedHat7 防火墙设置以及端口设置

    1.查看防火墙状态,root用户登录,执行命令systemctl status firewalld 2.开启防火墙:systemctl start firewalld 3.关闭防火墙:systemct ...

  4. Objective-C中NSString转NSNumber的方法

    本文转载至 http://www.linuxidc.com/Linux/2013-02/78866.htm 在Objective-C中,以数字格式组成的字符串经常需要转换为NSNumber对象后再使用 ...

  5. 九度OJ 1164:旋转矩阵 (矩阵运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3188 解决:1245 题目描述: 任意输入两个9阶以下矩阵,要求判断第二个是否是第一个的旋转矩阵,如果是,输出旋转角度(0.90.180. ...

  6. 九度OJ 1069:查找学生信息 (排序、查找)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11240 解决:3024 题目描述: 输入N个学生的信息,然后进行查询. 输入: 输入的第一行为N,即学生的个数(N<=1000) 接 ...

  7. AsyncHttpClien访问网络案例分析

    Android数据存储的四种方式分别是:SharedPreferences存储.File文件存储.Network网络存储和sqlite数据库存储,网络存储需要使用AsyncHttpClient发送请求 ...

  8. SPDIF接口细则详解

    链接:https://max.book118.com/html/2017/0422/101658483.shtm

  9. Java基础教程:多线程基础(2)——线程间的通信

    Java基础教程:多线程基础(2)——线程间的通信 使线程间进行通信后,系统之间的交互性会更强大,在大大提高CPU利用率的同时还会使程序员对各线程任务在处理的过程中进行有效的把控与监督. 线程间的通信 ...

  10. Java基础教程:多线程基础(3)——阻塞队列

    Java基础教程:多线程基础(3)——阻塞队列 快速开始 引入问题 生产者消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据. 模 ...