-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3291.cpp
More file actions
54 lines (44 loc) · 1.27 KB
/
Copy path3291.cpp
File metadata and controls
54 lines (44 loc) · 1.27 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
class Trie {
struct Node {
Node *children[26] = {0};
} root;
public:
void insert(const string &s) {
Node *crnt = &root;
for (const char c : s) {
const int idx = c - 'a';
if (!crnt->children[idx]) crnt->children[idx] = new Node();
crnt = crnt->children[idx];
}
}
int find(const string &s, int start) const {
const Node *crnt = &root;
int n = 0;
for (int i = start; i < size(s); i++, n++) {
const int idx = s[i] - 'a';
if (!crnt->children[idx]) break;
crnt = crnt->children[idx];
}
return n;
}
};
class Solution {
public:
int minValidStrings(const vector<string> &words, const string &target) const {
static unsigned dp[5001];
const int n = size(target);
Trie trie;
for (const auto &word : words)
trie.insert(word);
memset(dp, 0xFF, sizeof(dp));
dp[0] = 0;
for (int i = 0; i < n; i++) {
if (dp[i] == -1) continue;
const int limit = trie.find(target, i);
for (int len = 1; len <= limit; len++) {
dp[i + len] = min(dp[i + len], dp[i] + 1);
}
}
return dp[n];
}
};