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和golang的比较
1)垃圾回收GC 像 Java 一样,Go 的垃圾回收是全局的,这意味着一旦垃圾回收被触发,所有的 goroutine 都会被暂停,造成一段时间的业务延迟. Erlang 的垃圾回收是进程级别的,每一 ...
- android 用webView作为编辑器 各种问题
1.首先我要说明一下为什么要写这个博客,因为公司最近需要一个自定义的编辑器,苦于没有思路在网上找了好久,看到了好多android实现的编辑器(其实也就那么几个并不多),公司需求和网页端同步共享创建的文 ...
- 高特权级代码段转向低特权级代码段(利用 ret(retf) 指令实现 jmp from ring0 to ring3)
[0]写在前面 0.1)本代码旨在演示 从 ring0 转移到 ring3(即,从高特权级 转移到 低特权级) 0.2)本文 只对 与 门相关的 代码进行简要注释,言简意赅: 0.3)文末的个人总结是 ...
- window 安装 skywalking
1.下载安装包 官网下载需要的安装包: https://github.com/OpenSkywalking/skywalking/releases 分别下载skywalking-collector.z ...
- 查看远程分支的log
1 将远程分支的commit fetch到本地 git fetch 2 查看远程分支的log git log <remote-branch>
- 10分钟看懂, Java NIO 底层原理
目录 写在前面 1.1. Java IO读写原理 1.1.1. 内核缓冲与进程缓冲区 1.1.2. java IO读写的底层流程 1.2. 四种主要的IO模型 1.3. 同步阻塞IO(Blocking ...
- ES5中的类与继承
最近在重新复习TypeScript,看到类这块的时候自然会和ES5中的类写法进行对比加深印象. 发现ES5的类与继承一些细节还是挺多的,时间久了容易忘记,特此记录下. 首先是ES5的类定义,这没什么好 ...
- python列表(list)常用方法
#!/usr/bin/env python # -*- coding:utf-8 -*- a = [1, 2, 3, 4, 5] # 索引 print(a[0], a[1], a[2], a[3], ...
- Java for LeetCode 080 Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- DLL中导出ANSI和UNICODE函数
模仿window中的DLL导出ANSI和UNICODE版本的函数,使用UNICODE宏来控制使用哪个版本: 在函数实际的执行代码UNICODE版本中,在ANSI函数的版本中只做参数的转换,及ANSI字 ...