Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.
Note:
- The same word in the dictionary may be reused multiple times in the segmentation.
- You may assume the dictionary does not contain duplicate words.
Example 1:
Input:
s = "catsanddog",wordDict = ["cat", "cats", "and", "sand", "dog"]
Output:
[
"cats and dog",
"cat sand dog"
]
Example 2:
Input:
s = "pineapplepenapple",wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
Output:
[
"pine apple pen apple",
"pineapple pen apple",
"pine applepen apple"
]
Explanation: Note that you are allowed to reuse a dictionary word.
Example 3:
Input:
s = "catsandog",wordDict = ["cats", "dog", "sand", "and", "cat"]
Output:
[]
题解:
我的dfs和dp都tle,很无奈,实在是想不到哪里时间太高,看了一个回溯法
dfs超时
class Solution {
public:
void dfs (string s, int k, int n, int m, vector<string> &words, vector<string> &res, string idx) {
if (k == n) {
idx.pop_back();
res.push_back(idx);
}
for (int i = 0; i < m; i++) {
if (s.substr(k, words[i].length()) == words[i]) {
//cout << words[i] << endl;
idx += words[i] + " ";
dfs(s, k + words[i].length(), n, m, words, res, idx);
idx.pop_back();
idx.erase(idx.end() - words[i].length(), idx.end());
}
}
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
//sort(wordDict.begin(), wordDict.end());
int n = s.length();
int m = wordDict.size();
string idx;
vector<string> res;
dfs(s, 0, n, m, wordDict, res, idx);
return res;
}
};
dp超时
class Solution {
public:
vector<string> wordBreak(string s, vector<string> &dict) {
int n = s.length();
int m = dict.size();
set<string> st;
for (int i = 0; i < m; i++) {
st.insert(dict[i]);
}
if (n == 0 || m == 0) {
return vector<string>();
}
if (count(dict.begin(), dict.end(), s) != 0) {
return vector<string>(1, s);
}
vector<string> res;
//vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
vector<bool> dp(n + 1, false);
map<int, vector<string>> data;
for (int i = 0; i < n; i++) {
data.insert(make_pair(i + 1, vector<string>()));
for (int j = 1; j <= i; j++) {
string idx;
if (dp[j] == false) {
idx = s.substr(0, j);
if (st.find(idx) != st.end()) {
data[j].push_back(idx);
dp[j] = true;
}
}
idx = s.substr(j, i - j + 1);
if (st.find(idx) != st.end()) {
for (int x = 0; x < data[j].size(); x++) {
data[i + 1].push_back(data[j][x] + " " + idx);
}
}
}
}
return data[n];
}
};
回溯
class Solution {
public:
vector<string> getAns(string s, vector<string> &words, map<string, vector<string>> &data) {
if (data.find(s) != data.end()) {
return data[s];
}
if (s.length() == 0) {
return {""};
}
vector<string> res;
for (int i = 0; i < words.size(); i++) {
if (s.substr(0, words[i].length()) == words[i]) {
vector<string> tmp = getAns(s.substr(words[i].length()), words, data);
for (int j = 0; j < tmp.size(); j++) {
res.push_back(words[i] + (tmp[j].length() == 0 ? "":" ") + tmp[j]);
}
}
}
data[s] = res;
return data[s];
}
vector<string> wordBreak(string s, vector<string>& wordDict) {
map<string, vector<string> > data;
return getAns(s, wordDict, data);
}
};