#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define MAX 101010
#define MOD 1000000007
#define ln '\n'
signed main() {
ll N, M;
cin >> N >> M;
string s1, s2;
cin >> s1 >> s2;
vector<ll> arr;
arr.resize(N);
ll i, j;
for (i = 1; i < M; i++) {
vector<ll> pr;
for (j = 0; j < N; j++) {
if (s1[j] == s2[i - 1]) pr.push_back(j);
}
ll chk = 0;
for (j = 0; j < N; j++) {
if (s1[j] == s2[i]) {
arr[j] = 101010101010;
for (auto k : pr) {
if (k == j) continue;
chk = 1;
arr[j] = min(arr[j], abs(k - j) + arr[k]);
}
}
}
if (!chk) {
cout << -1 << ln;
return 0;
}
}
ll ans = 1010101010;
for (i = 0; i < N; i++) {
if (s1[i] == s2[M - 1]) ans = min(ans, arr[i]);
}
cout << ans << ln;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 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 |
7 ms |
288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |