이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mxN = 1e3 + 5, mod = 1e9 + 7;
const ll inf = 1e18;
int n, m;
ll dp[mxN][mxN];
string s, t;
int main() {
ios :: sync_with_stdio(false), cin.tie(nullptr);
cin >> n >> m >> s >> t;
for (int i = 0; i < n; ++i) {
for (int j = 1; j <= m; ++j)
dp[i][j] = inf;
}
for (int i = 0; i < n; ++i) {
if (s[i] == t[0])
dp[i][1] = 0;
}
for (int j = 2; j <= m; ++j) {
for (int i = 0; i < n; ++i) {
if (s[i] == t[j - 1]) {
for (int k = i - 1; k >= 0; k--) {
if (s[k] == t[j - 2])
dp[i][j] = min(dp[i][j], dp[k][j - 1] + (i - k));
}
for (int k = i + 1; k < n; ++k) {
if (s[k] == t[j - 2])
dp[i][j] = min(dp[i][j], dp[k][j - 1] + (k - i));
}
}
}
}
ll ans = inf;
for (int i = 0; i < n; ++i)
ans = min(ans, dp[i][m]);
if (ans == inf)
ans = -1;
cout << ans;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |