意甲冠军:推断字符串给定的字符串是否构成词典。

来推断目标字符串相匹配整个字典。我们需要来推断目标字符串的每个前缀开始的下一场比赛,这需要匹配目标字符串的成功,所有前缀的枚举。

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 特里的更多相关文章

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

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

  2. LeetCode:Word Break II(DP)

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

  3. LeetCode Word Break II

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

  4. [leetcode]Word Break II @ Python

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

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

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

  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拆分词语

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

  9. LeetCode: Word Break I && II

    I title: https://leetcode.com/problems/word-break/ Given a string s and a dictionary of words dict, ...

随机推荐

  1. REGEXP_LIKE,REGEXP_INSTR,REGEXP_SUBSTR,REGEXP_REPLACE

    参考: http://www.cnblogs.com/scottckt/archive/2012/10/11/2719562.html http://www.jb51.net/article/3842 ...

  2. .dmp文件导出使用示例

    exp导出的几种用例,先睹为快: 1 将数据库SampleDB完全导出,用户名system 密码manager 导出到E:/SampleDB.dmp中 exp system/manager@TestD ...

  3. Understanding Cubert Concepts(一)Partitioned Blocks

    Understanding Cubert Concepts(一)Partitioned Blocks Cubert Concepts 对于Cubert,我们要理解其核心的一些概念,比方BLOCK.这些 ...

  4. Mac安装brew及其用法

    Mac 安装 brew 及其用法: 安装brew: curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz - ...

  5. swift学习第十一天:类的定义

    一: 主要内容 类的介绍和定义 类的属性 类的构造函数 一. 类的介绍和定义 Swift也是一门面向对象开发的语言 面向对象的基础是类,类产生了对象 在Swift中如何定义类呢? class是Swif ...

  6. pppoe-环境下的mtu和mss

    路由器上在宽带拨号高级设置页面会有设置数据包MTU的页面 数据包MTU(字节):1480 (默认是1480,如非必要,请勿修改) PPPoE/ADSL:1492 ,可以尝试修改为1492 MTU: M ...

  7. Android 在Service里面启动Activity

    直接在代码: Intent dialogIntent = new Intent(getBaseContext(), YourActivity.class); dialogIntent.addFlags ...

  8. bash - Logical_OR

    转载 https://bash.cyberciti.biz/guide/Logical_OR Logical OR   ← Logical AND Home Logical Not ! → Logic ...

  9. CCPC2016长春站打铁记

    Day0 晚上到的长春.很冷.到了宾馆.放了行李.然后就去吃了点火锅.很好吃.在福建吃的都没有酱.但是回去后有点拉肚子..几个队友也有同样的反应.路过了吉大.拍了一张照片.哎.压力好大. Day1 来 ...

  10. 【40.17%】【codeforces 569B】Inventory

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...