-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_0139_wordBreak.cc
More file actions
134 lines (124 loc) · 2.88 KB
/
Problem_0139_wordBreak.cc
File metadata and controls
134 lines (124 loc) · 2.88 KB
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
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int process(string &s, int index, unordered_set<string> &set)
{
if (index == s.length())
{
return 1;
}
int ways = 0;
for (int end = index; end < s.length(); end++)
{
if (set.count(s.substr(index, end + 1 - index)))
{
ways += process(s, end + 1, set);
}
}
return ways;
}
// 递归
bool wordBreak1(string s, vector<string> &wordDict)
{
unordered_set<string> set(wordDict.begin(), wordDict.end());
return process(s, 0, set) != 0;
}
// 递归改dp
bool wordBreak2(string s, vector<string> &wordDict)
{
unordered_set<string> set(wordDict.begin(), wordDict.end());
int n = s.length();
vector<int> dp(n + 1);
dp[n] = 1;
for (int i = n - 1; i >= 0; i--)
{
for (int end = i; end < n; end++)
{
if (set.count(s.substr(i, end + 1 - i)))
{
dp[i] += dp[end + 1];
}
}
}
return dp[0] != 0;
}
class Node
{
public:
bool end;
unordered_map<int, Node *> nexts;
Node() { end = false; }
};
// 字典树优化dp
bool wordBreak3(string s, 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->end = true;
}
int N = s.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 = s[end] - 'a';
if (cur->nexts[path] == nullptr)
{
// 查询s[i..end]是否在字典树内
break;
}
cur = cur->nexts[path];
if (cur->end && dp[end + 1])
{
// 存在单词s[i..end],并且end+1位置后面也能分割成单词
dp[i] = true;
break;
}
}
}
return dp[0];
}
};
void testWordBreak()
{
Solution s;
vector<string> w1 = {"leet", "code"};
vector<string> w2 = {"apple", "pen"};
vector<string> w3 = {"cats", "dog", "sand", "and", "cat"};
EXPECT_TRUE(s.wordBreak1("leetcode", w1));
EXPECT_TRUE(s.wordBreak1("applepenapple", w2));
EXPECT_FALSE(s.wordBreak1("catsandog", w3));
EXPECT_TRUE(s.wordBreak2("leetcode", w1));
EXPECT_TRUE(s.wordBreak2("applepenapple", w2));
EXPECT_FALSE(s.wordBreak2("catsandog", w3));
EXPECT_TRUE(s.wordBreak3("leetcode", w1));
EXPECT_TRUE(s.wordBreak3("applepenapple", w2));
EXPECT_FALSE(s.wordBreak3("catsandog", w3));
EXPECT_SUMMARY;
}
int main()
{
testWordBreak();
return 0;
}