#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])
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 |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |