[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, ...
随机推荐
- mahout測试朴素贝叶斯分类样例
对于这个測试建议大家先理解原理,这里我画了例如以下的示意图 接下来就依照例如以下的细节来输入指令測试: 首先前提是Hadoop安装并启动,mahout已经安装了. <strong>< ...
- how to query for a list<String> in jdbctemplate?--转载
原文地址:http://stackoverflow.com/questions/13354158/how-to-query-for-a-liststring-in-jdbctemplate I'm ...
- [Angular] Angular Global Keyboard Handling With EventManager
If we want to add global event handler, we can use 'EventManager' from '@angular/platform-broswer'. ...
- java.util.logging使用笔记2
java.util.logging是java自带的日志处理系统,其功能不如log4j/logback强大,但可以完成基本的日志需求. 另外,如果在一个项目中使用log4j, 而这个项目引用的第3方 ...
- php 发送QQ邮箱邮件
这是我的源码比较简陋 https://www.lanzous.com/i2l7h8f 感谢 https://www.cnblogs.com/woider/p/6980456.html 下载phpmai ...
- 键盘钩子监测按键后,获取键码及按键名称(MFC)
LRESULT CALLBACK LowLevelKeyboardProc(int nCode,WPARAM wParam,LPARAM lParam){ if(nCode ==HC_ACTION & ...
- 通过双重for循环来找到JSON中不反复的数据
//通过双重for循环来找到JSON中不反复的数据 var count = 0; for ( i=0; i<json.length; i++) { for ( j=0; j<i; j++) ...
- vim修复,telnet安装启动,linux更新软件源
vim修复: 修复前提,你到UBUNTU能够联网.否则仅仅能卸载,不能安装 1.sudo apt-get remove vim-common 2.sudo apt-get install vim ...
- Boost.Asio c++ 网络编程翻译(10)
read/write方法 这些方法对一个流进行读写操作(能够是套接字,或者其它表现的像流的类): async_read(stream, buffer [, completion],handler):这 ...
- Linux系统编程——线程私有数据
在多线程程序中.常常要用全局变量来实现多个函数间的数据共享.因为数据空间是共享的,因此全局变量也为全部线程共同拥有. 測试代码例如以下: #include <stdio.h> #inclu ...