-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3291.cpp
More file actions
57 lines (57 loc) · 1.49 KB
/
3291.cpp
File metadata and controls
57 lines (57 loc) · 1.49 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
class TrieNode {
public:
vector<TrieNode*> children;
bool isEnd;
TrieNode() {
children.resize(26, nullptr);
isEnd = false;
}
};
class Trie {
public:
TrieNode* root;
Trie() {
root = new TrieNode();
}
void insert(string& s) {
int n = s.size();
TrieNode* curr = root;
for (int i = 0; i < n; ++i) {
if (curr->children[s[i] - 'a'] == nullptr) {
curr->children[s[i] - 'a'] = new TrieNode();
}
curr = curr->children[s[i] - 'a'];
}
curr->isEnd = true;
}
int search(string& s, int start) {
int res = 0;
int n = s.size();
TrieNode* curr = root;
for (int i = start; i < n; ++i) {
if (curr->children[s[i] - 'a'] == nullptr) {
return i - start;
}
curr = curr->children[s[i] - 'a'];
}
return n - start;
}
};
class Solution {
public:
int minValidStrings(vector<string>& words, string target) {
Trie* trie = new Trie();
for (auto& word : words) trie->insert(word);
int n = target.size();
vector<int> dp(n + 1, INT_MAX / 2);
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
int length = trie->search(target, i - 1);
for (int j = 0; j < length; ++j) {
dp[i + j] = min(dp[i + j], dp[i - 1] + 1);
}
}
if (dp[n] == INT_MAX / 2) return -1;
return dp[n];
}
};