-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode582.cpp
52 lines (43 loc) · 1.43 KB
/
lintcode582.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public:
/*
* @param s: A string
* @param wordDict: A set of words.
* @return: All possible sentences.
*/
vector<string> dfs(string &s,
unordered_map<string, vector<string>> &memo,
unordered_set<string> &wordDict) {
if(memo.count(s) != 0)
return memo[s];
vector<string> res;
if(s.length() == 0)
return res;
// s本身是否就是單字
if(wordDict.count(s) != 0)
res.push_back(s);
// 把s分解成單字
for(int len = 1; len < s.length(); ++len) {
string word = s.substr(0, len);
if(wordDict.count(word) == 0)
continue;
string suffix = s.substr(len);
vector<string> res_ = dfs(suffix, memo, wordDict);
for(auto i : res_) {
res.push_back(word + " " + i);
}
}
memo[s] = res;
return res;
}
vector<string> wordBreak(string &s, unordered_set<string> &wordDict) {
// write your code here
if(wordDict.size() == 0 || s.length() == 0)
return {};
vector<string> res;
// memo在word中有相同的單字時特別好用
unordered_map<string, vector<string>> memo;
res = dfs(s, memo, wordDict);
return res;
}
};