Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61c34e0

Browse files
dfs memo
1 parent fd046c1 commit 61c34e0

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Source : https://leetcode.com/problems/word-break-ii/
2+
// Author : henrytien
3+
// Date : 2022年02月03日
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence
8+
* where each word is a valid dictionary word. Return all such possible sentences in any order.
9+
*
10+
* Note that the same word in the dictionary may be reused multiple times in the segmentation.
11+
*
12+
* Example 1:
13+
*
14+
* Input: s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"]
15+
* Output: ["cats and dog","cat sand dog"]
16+
*
17+
* Example 2:
18+
*
19+
* Input: s = "pineapplepenapple", wordDict = ["apple","pen","applepen","pine","pineapple"]
20+
* Output: ["pine apple pen apple","pineapple pen apple","pine applepen apple"]
21+
* Explanation: Note that you are allowed to reuse a dictionary word.
22+
*
23+
* Example 3:
24+
*
25+
* Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
26+
* Output: []
27+
*
28+
* Constraints:
29+
*
30+
* 1 <= s.length <= 20
31+
* 1 <= wordDict.length <= 1000
32+
* 1 <= wordDict[i].length <= 10
33+
* s and wordDict[i] consist of only lowercase English letters.
34+
* All the strings of wordDict are unique.
35+
******************************************************************************************************/
36+
37+
#include "../inc/ac.h"
38+
class Solution
39+
{
40+
public:
41+
vector<string> wordBreak(string s, vector<string> &wordDict)
42+
{
43+
unordered_map<int, vector<string>> memo{{s.size(), {""}}};
44+
45+
function<vector<string>(int i)> sentences = [&](int i)
46+
{
47+
if (!memo.count(i))
48+
{
49+
for (int j = i + 1; j <= s.size(); j++)
50+
{
51+
string token = s.substr(i, j - i);
52+
if (std::count(wordDict.begin(), wordDict.end(), token))
53+
{
54+
for (auto &&tail : sentences(j))
55+
{
56+
memo[i].push_back(token + (tail == "" ? "" : ' ' + tail));
57+
}
58+
}
59+
}
60+
}
61+
return memo[i];
62+
};
63+
return sentences(0);
64+
}
65+
};
66+
67+
int main()
68+
{
69+
70+
string s = "catsanddog";
71+
vector<string> word_dict = {"cat", "cats", "and", "sand", "dog"};
72+
// string s = "pineapplepenapple";
73+
// vector<string> word_dict = {"apple", "pen", "applepen", "pine", "pineapple"};
74+
// string s = "catsandog";
75+
// vector<string> word_dict = {"cats", "dog", "sand", "and", "cat"};
76+
auto v = Solution().wordBreak(s, word_dict);
77+
for (auto &&iter : v)
78+
cout << iter << " ";
79+
cout << "\n";
80+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /