Submission #460547

# Submission time Handle Problem Language Result Execution time Memory
460547 2021-08-09T07:31:54 Z benson0402 Bajka (COCI20_bajka) C++14
70 / 70
27 ms 316 KB
#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(a[k] == b[i]) {
					if(k + 1 < n && a[k + 1] == b[i - 1])
						dp[k] = min(dp[k], dp2[j] + abs(j - (k + 1)) + 1);
					if(k - 1 >= 0 && a[k - 1] == b[i - 1])
						dp[k] = min(dp[k], dp2[j] + abs(j - (k - 1)) + 1);
				}
			}
		}
		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 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 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 316 KB Output is correct
7 Correct 7 ms 204 KB Output is correct
8 Correct 27 ms 204 KB Output is correct
9 Correct 21 ms 316 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 4 ms 204 KB Output is correct