-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
96 lines (87 loc) · 2.49 KB
/
Solution.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class Node {
public:
Node* children[26];
int v = -1;
Node() {
fill(children, children + 26, nullptr);
}
};
class Solution {
private:
const long long inf = 1LL << 60;
Node* root = new Node();
int idx;
vector<vector<long long>> g;
string s;
string t;
vector<long long> f;
public:
long long minimumCost(string source, string target, vector<string>& original, vector<string>& changed, vector<int>& cost) {
int m = cost.size();
g = vector<vector<long long>>(m << 1, vector<long long>(m << 1, inf));
s = source;
t = target;
for (int i = 0; i < g.size(); ++i) {
g[i][i] = 0;
}
for (int i = 0; i < m; ++i) {
int x = insert(original[i]);
int y = insert(changed[i]);
g[x][y] = min(g[x][y], static_cast<long long>(cost[i]));
}
for (int k = 0; k < idx; ++k) {
for (int i = 0; i < idx; ++i) {
if (g[i][k] >= inf) {
continue;
}
for (int j = 0; j < idx; ++j) {
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
f = vector<long long>(s.length(), -1);
long long ans = dfs(0);
return ans >= inf ? -1 : ans;
}
private:
int insert(const string& w) {
Node* node = root;
for (char c : w) {
int i = c - 'a';
if (node->children[i] == nullptr) {
node->children[i] = new Node();
}
node = node->children[i];
}
if (node->v < 0) {
node->v = idx++;
}
return node->v;
}
long long dfs(int i) {
if (i >= s.length()) {
return 0;
}
if (f[i] != -1) {
return f[i];
}
long long res = (s[i] == t[i]) ? dfs(i + 1) : inf;
Node* p = root;
Node* q = root;
for (int j = i; j < s.length(); ++j) {
p = p->children[s[j] - 'a'];
q = q->children[t[j] - 'a'];
if (p == nullptr || q == nullptr) {
break;
}
if (p->v < 0 || q->v < 0) {
continue;
}
long long temp = g[p->v][q->v];
if (temp < inf) {
res = min(res, temp + dfs(j + 1));
}
}
return f[i] = res;
}
};