Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
For example, given
s = "catsanddog"
,
dict = ["cat", "cats", "and", "sand", "dog"]
.
A solution is ["cats and dog", "cat sand dog"]
.
分析:
解分为三步:
(1)构造两级向量(vector<vector<int> > v)
链式存放前一个字符的位置。
(2)基于向量v逆向递归寻找词,借助栈
[dog --> [dog, sand --> [dog, sand, cat
[dog, and --> [dog, and, cats
(3)出栈时词以空格隔开,存入ret向量
"cat sand dog"
"cats and dog"
ac代码:
class Solution { public: vector<string> wordBreak(string s, unordered_set<string>& wordDict) { vector<string> ret; string news = "0" + s; int n = news.size(); vector<vector<int> > v(n); vector<bool> bpos(n, false); bpos[0] = true; for(int i = 1; i < n; i ++) { for(int j = 0; j < i; j ++) { if(bpos[j] == true && wordDict.find(news.substr(j+1, i-j)) != wordDict.end()) { bpos[i] = true; v[i].push_back(j); } } } if(bpos[n-1] == false) return ret; else { stack<string> stk; genRet(ret, news, v, stk, n-1); return ret; } } void genRet(vector<string>& ret, string news, vector<vector<int> > v, stack<string> stk, int bpos) { if(bpos == 0) {// generate final string string str; while(!stk.empty()) { string top = stk.top(); stk.pop(); str += (top + " "); } str.erase(str.end()-1); ret.push_back(str); } else { for(int i = 0; i < v[bpos].size(); i ++) { string cur = news.substr(v[bpos][i]+1, bpos-v[bpos][i]); stk.push(cur); genRet(ret, news, v, stk, v[bpos][i]); stk.pop(); } } } };
mycode:
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
int i,j,L=s.length();
vector<string>ret;
vector<vector<int>>v(L+1);
stack<string>stk;
bool word[L+1];
for(i=1;i<=L;i++)
word[i]=false;
word[0]=true;
for(i=1;i<=L;i++)
{
for(j=0;j<i;j++)
{
if(word[j]&&wordDict.find(s.substr(j,i-j))!=wordDict.end())
{
word[i]=true;
v[i].push_back(j);
}
}
}
if(!word[L])
return ret;
else
{
genRet(ret,s,v,stk,L);
return ret;
}
}
void genRet(vector<string>&ret,string s,vector<vector<int>>&v,stack<string>&stk,int L)
{
if(L==0)
{
string str;
while(!stk.empty())
{
string top=stk.top();
stk.pop();
str+=top+" ";
}
str.erase(str.end()-1);
ret.push_back(str);
}
else
{
for(int i=0;i<v[L].size();i++)
{
string tem=s.substr(v[L][i],L-v[L][i]+1);
stk.push(tem);
genRet(ret,s,v,stk,v[L][i]);
stk.pop();
}
}
}
};