-
Notifications
You must be signed in to change notification settings - Fork 1
/
139.cpp
81 lines (74 loc) · 2.13 KB
/
139.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
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
#include<bits/stdc++.h>
using namespace std;
class TrieNode{
public:
TrieNode* next[26];
bool isword;
TrieNode() {
fill(next, next+26, nullptr);
isword = false;
}
~TrieNode() {
for (int i = 0; i < 26; ++i)
if (next[i]) delete next[i];
}
};
class Solution {
public:
void insert(const string &s, TrieNode* root) {
int n = s.size();
for (int i = 0; i < n; ++i) {
int idx = s[i] - 'a';
if (!root->next[idx]) root->next[idx] = new TrieNode();
root = root->next[idx];
}
root->isword = true;
}
bool search(const string &s, TrieNode* root) {
int n = s.size();
for (int i = 0; i < n; ++i) {
int idx = s[i] - 'a';
if (!root->next[idx]) return false;
root = root->next[idx];
}
return root && root->isword;
}
bool findWord(const string &s, TrieNode* root, int begin, vector<bool> &hash) {
int n = s.size();
if (begin == n) return true;
TrieNode* p = root;
bool flag = false;
for (int i = begin; i < n; ++i) {
int idx = s[i] - 'a';
if (!p->next[idx]) {
if (p->isword) {
p = root; // 找到一个
--i;
continue;
} else {
return flag;
}
}
if (p->next[idx]->isword) {
if (!hash[i]) {
hash[i] = true;
flag = flag || findWord(s, root, i+1, hash); // 如果完成之后就在进行
}
}
p = p->next[idx];
}
return (p && p->isword) || flag;
}
bool wordBreak(string s, vector<string>& wordDict) {
TrieNode* root = new TrieNode(); // 根节点
for (const string &_s : wordDict) insert(_s, root);
vector<bool> ht(s.size(), false);
return findWord(s, root, 0, ht);
}
};
int main()
{
Solution s;
vector<string> dict = {"a","abc","b","cd"};
printf("%d\n", s.wordBreak("abcd", dict));
}