This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ST first
#define ND second
#define PB push_back
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
const int nax = 310, INF = 1e9;
int n, m;
string a, b;
int dp[nax][nax];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> a >> b;
for(int i = m - 1; i >= 1; --i) {
for(int j = 0; j < n; ++j) {
dp[i][j] = INF;
if(j > 0 && a[j - 1] == b[i]) {
dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + 1);
}
if(j < n - 1 && a[j + 1] == b[i]) {
dp[i][j] = min(dp[i][j], dp[i + 1][j + 1] + 1);
}
}
for(int j = 0; j < n; ++j) {
for(int k = 0; k < n; ++k) {
if(a[k] != a[j]) continue;
dp[i][j] = min(dp[i][j], dp[i][k] + abs(k - j));
}
}
}
int ans = INF;
for(int i = 0; i < n; ++i) {
if(a[i] == b[0]) {
ans = min(ans, dp[1][i]);
}
}
if(ans == INF) cout << "-1";
else cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |