This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define F first
#define S second
#define ALL(x) x.begin(), x.end()
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
#define eb emplace_back
#define ep emplace
#define mkp make_pair
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
/*------------------------------------------------------------------*/
const int N = 300 + 5;
int dp[N], dp2[N];
inline void solve() {
int n, m; cin >> n >> m;
string a, b; cin >> a >> b;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < N; ++j) dp[j] = INF;
for(int j = 0; j < n; ++j) {
if(!i && a[j] == b[i]) dp[j] = 0;
else if(i && dp2[j] != INF) {
for(int k = 0; k < n; ++k) if(j != k && a[k] == b[i] && ((k + 1 < n && a[k + 1] == a[j]) || (k - 1 >= 0 && a[k - 1] == a[j])))
dp[k] = min(dp[k], dp2[j] + abs(j - k));
}
}
copy(dp, dp + N, dp2);
}
int ans = INF;
for(int i = 0; i < n; ++i)
ans = min(ans, dp2[i]);
cout << (ans == INF ? -1 : ans) << '\n';
}
int main() {
cin.tie(0), ios::sync_with_stdio(0);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |