-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_0140_wordBreak.cc
179 lines (166 loc) · 4.02 KB
/
Problem_0140_wordBreak.cc
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <algorithm>
#include <iostream>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
void process(string &s, int index, string &path, vector<string> &ans, unordered_set<string> &set)
{
if (index == s.length())
{
ans.push_back(path.substr(1));
return;
}
for (int end = index; end < s.length(); end++)
{
string cur = s.substr(index, end + 1 - index);
if (set.count(cur))
{
path.append(" " + cur);
process(s, end + 1, path, ans, set);
path.erase(path.find_last_of(' '));
}
}
}
// 递归解
vector<string> wordBreak1(string s, vector<string> &wordDict)
{
unordered_set<string> set(wordDict.begin(), wordDict.end());
vector<string> ans;
string path;
process(s, 0, path, ans, set);
return ans;
}
class Node
{
public:
string path;
bool end;
unordered_map<int, Node *> nexts;
Node()
{
end = false;
}
};
vector<string> wordBreak2(string s, vector<string> &wordDict)
{
Node *root = gettrie(wordDict);
vector<bool> dp = getdp(s, root);
vector<string> path;
vector<string> ans;
process(s, 0, root, dp, path, ans);
return ans;
}
void process(string &str, int index, Node *root, vector<bool> &dp, vector<string> &path, vector<string> &ans)
{
if (index == str.length())
{
string cur;
for (int i = 0; i < path.size() - 1; i++)
{
cur.append(path.at(i) + " ");
}
cur.append(path.back());
ans.push_back(cur);
}
else
{
Node *cur = root;
for (int end = index; end < str.length(); end++)
{
int road = str[end] - 'a';
if (!cur->nexts.count(road))
{
break;
}
cur = cur->nexts[road];
if (cur->end && dp[end + 1])
{
path.push_back(cur->path);
process(str, end + 1, root, dp, path, ans);
path.pop_back();
}
}
}
}
Node *gettrie(vector<string> &wordDict)
{
Node *root = new Node();
for (string &str : wordDict)
{
Node *node = root;
int index = 0;
for (int i = 0; i < str.length(); i++)
{
index = str[i] - 'a';
if (!node->nexts.count(index))
{
node->nexts[index] = new Node();
}
node = node->nexts[index];
}
node->path = str;
node->end = true;
}
return root;
}
vector<bool> getdp(string &str, Node *root)
{
int N = str.length();
vector<bool> dp(N + 1);
dp[N] = true;
for (int i = N - 1; i >= 0; i--)
{
Node *cur = root;
for (int end = i; end < N; end++)
{
int path = str[end] - 'a';
if (!cur->nexts.count(path))
{
break;
}
cur = cur->nexts[path];
if (cur->end && dp[end + 1])
{
dp[i] = true;
break;
}
}
}
return dp;
}
};
bool isVectorEqual(vector<string> a, vector<string> b)
{
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
return a == b;
}
void testWordBreak()
{
Solution s;
vector<string> w1 = {"cat", "cats", "and", "sand", "dog"};
vector<string> o1 = {"cats and dog", "cat sand dog"};
vector<string> w2 = {"apple", "pen", "applepen", "pine", "pineapple"};
vector<string> o2 = {"pine apple pen apple", "pineapple pen apple", "pine applepen apple"};
vector<string> w3 = {"cats", "dog", "sand", "and", "cat"};
vector<string> o3;
EXPECT_TRUE(isVectorEqual(o1, s.wordBreak1("catsanddog", w1)));
EXPECT_TRUE(isVectorEqual(o2, s.wordBreak1("pineapplepenapple", w2)));
EXPECT_TRUE(isVectorEqual(o3, s.wordBreak1("catsandog", w3)));
EXPECT_TRUE(isVectorEqual(o1, s.wordBreak2("catsanddog", w1)));
EXPECT_TRUE(isVectorEqual(o2, s.wordBreak2("pineapplepenapple", w2)));
EXPECT_TRUE(isVectorEqual(o3, s.wordBreak2("catsandog", w3)));
EXPECT_SUMMARY;
}
int main()
{
testWordBreak();
return 0;
}