[LeetCode]Word Break 特里
意甲冠军:推断字符串给定的字符串是否构成词典。
来推断目标字符串相匹配整个字典。我们需要来推断目标字符串的每个前缀开始的下一场比赛,这需要匹配目标字符串的成功,所有前缀的枚举。
class TrieNode{//from http://www.cnblogs.com/x1957/p/3492926.html
public:
TrieNode* ch[26];//char指针数组
bool isWord;
TrieNode():isWord(false){
memset(ch,0,sizeof(TrieNode*)*26);
}
void insert(const string& ps){
TrieNode*q=this;
int id;
const char* p=ps.c_str();
while(*p){
id=p[0]-'a';
if(NULL==q->ch[id])
q->ch[id]=new TrieNode();
q=q->ch[id];
p++;
}
q->isWord=true;//是一个前缀
}
~TrieNode(){
for(int i=0;i<26;++i)
delete ch[i];
}
};
class Solution {
public:
bool *find;
TrieNode *root;
void match(string &s,int st,int ed){
TrieNode*p=root;
for(int i=st;i<ed;++i){
if(p->ch[s[i]-'a']){
p=p->ch[s[i]-'a'];
if(p->isWord)find[i]=true;
}
else break;
}
}
bool wordBreak(string s, unordered_set<string> &dict) {
int i,n=s.size();
unordered_set<string>::iterator bg,ed;
root=new TrieNode();
for(bg=dict.begin(),ed=dict.end();bg!=ed;bg++){
root->insert(*bg);
}
find=new bool[n];
memset(find,0,sizeof(bool)*n);
//先匹配前缀
match(s,0,n);
//再从全部匹配的单词開始接下去匹配
for(i=0;i<n&&find[n-1]==false;++i)
if(find[i])
match(s,i+1,n);
bool ans=find[n-1];
delete[]find;
delete root;
return ans;
}
};
DP版:
一个串AB可看成两个子串A、B构成。假设A和B都匹配。则AB匹配。用dp[i]表示前缀(0,i)是否匹配,则dp[n]=dp[0,k]&dp[k+1,n],k∈[0,n]。
这里dp[0,k]比較easy求,要推断后缀dp[k+1,n]是否在dict中。除了通过set.find(dp[k+1,n])不知还有什么办法。
bool wordBreak(string s, unordered_set<string> &dict) {
int i,j,n=s.size();
bool *dp=new bool[s.size()];
memset(dp,0,sizeof(bool)*n);
for(i=0;i<n;++i){
dp[i]=dict.find(s.substr(0,i+1))!=dict.end();
for(j=0;j<i&&!dp[i];++j){
dp[i]=(dp[j]&(dict.find(s.substr(j+1,i-j))!=dict.end()));
}
}
bool ans=dp[n-1];
delete[]dp;
return ans;
}
问题2:输出全部匹配的字符串 https://oj.leetcode.com/problems/word-break-ii/
方法:在每一个匹配的时候,针对每一个匹配的子串,记录能够匹配到该位置的子串起始位置。这样串中的一个字符就可能有多种匹配结果。
仅仅要由后往前回溯输出全部可能就可以。
class TrieNode{
public:
TrieNode*child[26];
bool isWord;
TrieNode():isWord(false){
memset(child,0,sizeof(TrieNode*)*26);
}
void insert(const string &str){
int n=str.size(),i,id;
TrieNode*p=this;
for(i=0;i<n;++i){
id= str[i]-'a';
if(!p->child[id]) {
p->child[id]=new TrieNode();
}
p=p->child[id];
}
p->isWord=true;
}
~TrieNode(){
for(int i=0;i<26;++i){
delete child[i];
child[i]=NULL;
}
}
};
class Solution{
public:
TrieNode*root;
bool *find;
vector<string>ans;
void match(string &str,int st,int ed,vector<set<int> >&pos){
int i,id;
TrieNode*p=root;
for(i=st;i<=ed;++i){
id=str[i]-'a';
if(p->child[id]){
p=p->child[id];
if(p->isWord){
find[i]=true;
pos[i].insert(st);
}
}
else break;
}
}
void dfs(string &str,int id,vector<set<int> >&pos,int *b,int len){
if(id<0){
string tmp;
for(int i=len-1;i>0;--i){
if(i<len-1)tmp+=" ";
tmp+=str.substr(b[i],b[i-1]-b[i]);
}
ans.push_back(tmp);
return;
}
set<int>::iterator bg=pos[id].begin(),ed=pos[id].end();
for(;bg!=ed;bg++){
b[len]=*bg;
dfs(str,b[len]-1,pos,b,len+1);
}
}
vector<string>wordBreak(string s, unordered_set<string>&dict){
if(s.size()==0){
return vector<string>();
}
unordered_set<string>::iterator bg,ed;
root=new TrieNode();
int n=s.size(),i;
for(bg=dict.begin(),ed=dict.end();bg!=ed;bg++){
root->insert(*bg);
}
find=new bool[n];
vector<set<int> >pos(n);
int *b=new int[n+2];
memset(find,0,sizeof(bool)*n);
match(s,0,n-1,pos);
for(i=0;i<n;++i){
if(find[i])
match(s,i+1,n-1,pos);
}
int x=0;
// cout<<pos[6].size();
if(find[n-1]){
b[x++]=n;
dfs(s,n-1,pos,b,x);
}
delete[] find;
delete[] b;
return ans;
}
};
版权声明:本文博主原创文章,博客,未经同意不得转载。
[LeetCode]Word Break 特里的更多相关文章
- [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 II
原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a string s and a dictionary of words ...
- [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 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 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 解题思路
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [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 I && II
I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...
随机推荐
- 让自己的软件实现拖拽打开文件(覆盖WM_DROPFILES,使用DragQueryFile,DragFinish API函数)
作者: 帅宏军 //声明 protected procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES; --------- ...
- KDE Plasma 5.8 的 LTS 周期正好与其所采用的 Qt 5.6 的 LTS 周期一致
在 KDE Plasma 5.7 刚刚发布不久,KDE 开发团队就宣布了 KDE Plasma 5.8 的开发计划.这个版本将是一个 LTS 版本,据我所知,这应该是 KDE 历史上第一个 LTS 版 ...
- python中有关字符串的处理
原文 Python 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字 ...
- OpenCV从入门到放弃(五):像素!
一.概念 1.图像本质上面是由数值组成的矩阵.矩阵中的一个元素相应一个像素. 2.对于灰度图像(黑白图像),像素是8位无符号数(CV_8U).0表示黑色,255表示白色.对于彩色图像,是用三原色数据合 ...
- JS中给函数参数添加默认值(多看课程)
JS中给函数参数添加默认值(多看课程) 一.总结 一句话总结:咋函数里面是可以很方便的获取调用函数的参数的,做个判断就好,应该有简便方法,看课程. 二.JS中给函数参数添加默认值 最近在Codewar ...
- Windows10终端优化方案:Ubuntu子系统+cmder+oh-my-zsh
原问地址:https://zhuanlan.zhihu.com/p/34152045 最近从MacBook换到了种草已久的Surface Book 2,而我的工作环境也自然要从macOS换到Windo ...
- ArcGIS Engine 编辑- ITask
转自原文ArcGIS Engine 编辑- ITask 下面的代码是我们定制的一个工作流-给等高线赋值 namespace EngineApplication { [Guid("5b0c06 ...
- ios开发日期的NSDate,NSCalendar分类
#import <Foundation/Foundation.h> @interface NSDate (XMGExtension) /** */ // @property (nonato ...
- html5 Canvas和SVG的区别是什么(总结)
html5 Canvas和SVG的区别是什么(总结) 一.总结 一句话总结:都是2D做图,svg是矢量图,canvas是位图.Canvas 是逐像素进行渲染的,适合游戏. 1.svg的全称是什么? S ...
- Django之富文本编辑器kindeditor 及上传
1.什么是富文本编辑器 百度百科(https://baike.baidu.com/item/%E5%AF%8C%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8 ...