-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfreedom_trail.cpp
More file actions
27 lines (25 loc) · 833 Bytes
/
freedom_trail.cpp
File metadata and controls
27 lines (25 loc) · 833 Bytes
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
// 自由之路
// https://leetcode.cn/problems/freedom-trail
// INLINE ../../images/search/freedom_trail.jpeg
#include <headers.hpp>
class Solution {
public:
int findRotateSteps(string ring, string key) {
array<vector<int>, 26> idx_list;
int n = ring.size(), m = key.size(), pc = ring.front() - 'a';
for (int i = 0; i < n; ++i)
idx_list[ring[i] - 'a'].push_back(i);
vector<int> dp(n, 0x3f3f3f3f), pre(n, 0x3f3f3f3f);
pre[0] = 0;
for (int i = 0; i < m; ++i) {
for (int idx : idx_list[key[i] - 'a'])
for (int pi : idx_list[pc])
dp[idx] =
min(dp[idx], pre[pi] + min(abs(pi - idx), n - abs(pi - idx)));
pre = std::move(dp);
dp = vector(n, 0x3f3f3f3f);
pc = key[i] - 'a';
}
return *min_element(pre.begin(), pre.end()) + key.size();
}
};