[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, ...
随机推荐
- MySQL參数binlog-do-db对binlogs写入的影响
1. 环境描写叙述 目的:当数据库中设置了binlog-do-db时.在不同的binlog_format=statement | row | mixed 下对binlog的写入影响,这个在主从复制中会 ...
- Spring Boot 静态资源处理(转)
Spring Boot 静态资源处理 Spring Boot 系列 Spring Boot 入门 Spring Boot 属性配置和使用 Spring Boot 集成MyBatis Spring Bo ...
- POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!
水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...
- 原生js螺旋运动
window.onload=function(){ var oSpiral=document.getElementById('spiral'); var oUl=oSpiral.getElements ...
- swift3.0调用相册
swift3.0调用相册首先需要注意: 1.swift3.0中调用相机和相册会导致崩溃 1.需要在info.plist文件中加入两个键值对,如下: /// 都是String类型,内容任意的字符串即可 ...
- Fragment之一:基本原理 分类: H1_ANDROID 2013-11-18 14:15 1642人阅读 评论(0) 收藏
1.低版本API对Fragment的支持 Fragment必须被加载进Acitivity中,才能呈现.而在低于3.0版本的API中,由于不存在Fragment,因此必须使用support包: (1)对 ...
- js实现金额小写转大写
function convertCurrency(currencyDigits) { var MAXIMUM_NUMBER = 1000000000000.00; var CN_ZERO = &quo ...
- 从多路搜索树到 B-树
1. 什么是 B 树 B 树是为磁盘或其他直接存取的辅助存储设备而设计的一种平衡二叉树: B 树类似于红黑树,但它们在降低磁盘 I/O 操作数方面要更好一点, 许多数据库系统使用 B 树或者 B 树的 ...
- 数据局部性(data locality)
信息处理的典型模式是,将所有数据项视为一个集合,并将其组织为适宜的数据结构(或者说使用适宜的数据结构对之进行存储以及组织),进而借助操作接口高效访问. 为了考查和评价各操作接口的效率,除了从最坏情况的 ...
- 解决“不是有效的win32应用程序”问题
http://blog.csdn.net/shuaihj/article/details/17096903