#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
using namespace std;
const int man = (int)(3e2 + 10);
const int mam = (int)(3e2 + 10);
int n, m;
string s, t;
int dp[man][mam];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef _LOCAL
in("inB.txt");
out("outB.txt");
#endif
cin >> n >> m;
cin >> s;
cin >> t;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
dp[i][j] = 1e9;
}
}
for (int i = 1; i <= n; ++i) {
if (s[i - 1] == t[0]) {
dp[1][i] = 0;
}
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j <= n; ++j) {
if (s[j - 1] != t[i - 1]) {
continue;
}
for (int k = 1; k <= n; ++k) {
if (j == k) {
continue;
}
if (s[k - 1] != t[i]) {
continue;
}
dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + abs(j - k));
}
}
}
int ans = 1e9;
for (int i = 1; i <= n; ++i) {
if (s[i - 1] != t.back()) {
continue;
}
ans = min(ans, dp[m][i]);
}
if (ans == 1e9) {
cout << "-1\n";
} else {
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |