Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode"
,
dict = ["leet", "code"]
.
Return true because "leetcode"
can be segmented as "leet code"
.
建字典树搞。
class Solution {
public:
class Node {
public:
Node* next[26];
bool end;
Node(): end(false) { for (int i = 0; i < 26; i++) next[i] = NULL;}
void insert(string a) {
Node * cur = this;
for (int i = 0; i < a.size(); i++) {
if (cur->next[a[i]-'a'] == NULL) {
cur->next[a[i]-'a'] = new Node();
}
cur = cur->next[a[i]-'a'];
}
cur->end = true;
}
~Node () {
for (int i = 0;i < 26; i++) delete next[i];
}
};
bool wordBreak(string s, unordered_set<string> &dict) {
Node root;
for (auto it = dict.begin(); it != dict.end(); ++it) {
root.insert(*it);
}
vector<bool> v(s.size(), false);
findMatch(s, &root, 0, v);
for (int i = 0; i < s.size(); i++)
if (v[i]) findMatch(s, &root, i+1, v);
return v[s.size() - 1];
}
void findMatch(const string& s, Node* cur, int start, vector<bool> &v) {
int i = start, n = s.size();
while (i < n) {
if (cur->next[s[i] - 'a'] != NULL) {
if (cur->next[s[i] - 'a']->end) v[i] = true;
cur = cur->next[s[i] - 'a'];
}
else break;
i++;
}
}
};
粗暴一点也是ok的。
class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
int n = dict.size();
int maxlen = 0;
for (auto it = dict.begin(); it != dict.end(); ++it)
if (it->size() > maxlen) maxlen = it->size();
int sn = s.size();
vector<bool> v(sn, false);
for (int i = 0; i < sn; i++) {
if (i == 0 || (i > 0 && v[i-1])) {
for (int j = 1; j <= maxlen && i + j - 1 < sn; j++) {
if (dict.count(s.substr(i,j)) > 0) v[i+j-1] = true;
}
}
}
return v[sn-1];
}
};
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"]
.
class Solution {
public:
vector<string> wordBreak(string s, unordered_set<string> &dict) {
vector<string> res;
int dn = dict.size();
int sn = s.size();
int maxlen = 0;
for (auto it = dict.begin(); it != dict.end(); ++it) {
if (it->size() > maxlen) maxlen = it->size();
}
vector<vector<int> > next(sn);
vector<bool> v(sn, false);
for (int i = 0; i < sn; i++) {
if (i == 0 || (i > 0 && v[i-1])) {
for (int j = 1; j <= maxlen && i+j-1 < sn; j++) {
if (dict.count(s.substr(i, j)) > 0) {
v[i+j-1] = true;
next[i].push_back(j);
}
}
}
}
if (!v[sn-1]) return res;
vector<int> x;
x.push_back(0);
gen(s, res, next, x);
return res;
}
void gen(const string& s, vector<string>& res, vector<vector<int> >& next, vector<int> &v) {
int cur = v.back();
if (cur == s.size()) {
string t = s.substr(v[0], v[1] - v[0]);
for (int i = 1; i < v.size() - 1; i++)
t += " " + s.substr(v[i], v[i+1] - v[i]);
res.push_back(t);
return;
}
for (int i = 0; i < next[cur].size(); i++) {
v.push_back(cur+next[cur][i]);
gen(s, res, next, v);
v.pop_back();
}
}
};