# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
851406 |
2023-09-19T17:43:23 Z |
vjudge1 |
Bajka (COCI20_bajka) |
C++17 |
|
74 ms |
860 KB |
//author:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define ONLINE_JUDGE
const int INF = 1e9 + 7;
const int MAXN = 300 + 5;
int dp[MAXN][MAXN];
void solve() {
memset(dp, -1, sizeof(dp));
int n, m;
cin >> n >> m;
string a, b;
cin >> a >> b;
a = '$' + a;
a = a + '$';
b = '$' + b;
b = b + '$';
function <int(int, int)> f = [&](int x, int al) -> int {
if(dp[x][al] != -1)
return dp[x][al];
if(al == m +1)
return 0;
int res = INF;
for(int i = 1; i <= n; i++) {
if(a[i] == b[al]) {
if(abs(i - x) == 1) {
res = min(res, f(i, al +1) + abs(i - x));
}
if(a[i +1] == a[x] && abs((i +1) - x) != 0) {
res = min(res, f(i, al +1) + abs((i +1) - x) + abs((i +1) - i));
}
if(a[i -1] == a[x] && abs((i -1) - x) != 0) {
res = min(res, f(i, al +1) + abs((i -1) - x) + abs((i -1) - i));
}
}
}
return dp[x][al] = res;
};
int ans = INF;
for(int i = 1; i <= n; i++) {
if(a[i] == b[1]) {
ans = min(ans, f(i, 2));
}
}
cout << (ans == INF ? -1 : ans);
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
while(t--)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
820 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
824 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
860 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
820 KB |
Output is correct |
7 |
Correct |
8 ms |
860 KB |
Output is correct |
8 |
Correct |
48 ms |
844 KB |
Output is correct |
9 |
Correct |
74 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
652 KB |
Output is correct |