-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path139backtrack.h
58 lines (52 loc) · 1.55 KB
/
139backtrack.h
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
//
// Created by 17336 on 2021/11/2.
//
#ifndef HOT100_139_H
#define HOT100_139_H
#endif //HOT100_139_H
#include "string"
#include "vector"
#include "unordered_set"
using namespace std;
//字典树
struct Tire {
Tire *next[26] = {nullptr};
bool now_end = false;
public:
//插入字符串word
void insert(const string &word) {
Tire *p = this;
for (const auto &item: word) {
if (p->next[item - 'a'] == nullptr) p->next[item - 'a'] = new Tire();
p = p->next[item - 'a'];
}
p->now_end = true;
}
};
class Solution {
//备忘录,用于存放已经判断过的不可拆分的字符串(可拆分的不用存,因为一旦可拆分一定return true了)
unordered_set<int> memo;
Tire *root = new Tire();
public:
bool wordBreak(string s, vector<string> &wordDict) {
for (const auto &item: wordDict) {
root->insert(item);
}
return backtrack(s,0);
}
bool backtrack(const string &s,int x) {
Tire *p = root;
for (int i = x; i < s.size(); i++) {
//如果某个字符在字典树中不存在,则以x起始的子串不可拆分,记录x
if (p->next[s[i] - 'a'] == nullptr){
memo.insert(x);
return false;
}
p = p->next[s[i] - 'a'];
if (p->now_end && (i==s.size()-1||(!memo.count(i+1)&&backtrack(s,i+1))))return true;
}
//如果最后一个字符判断完p->now_end为false
memo.insert(x);
return false;
}
};