Submission #377629

# Submission time Handle Problem Language Result Execution time Memory
377629 2021-03-14T13:48:13 Z dimashii Bajka (COCI20_bajka) C++17
70 / 70
101 ms 2412 KB
#include <bits/stdc++.h>
 
#define ll long long
 
using namespace std;
 
const int mxN = 1e3 + 5, mod = 1e9 + 7;
 
const ll inf = 1e18;
 
int n, m;
 
ll dp[mxN][mxN];
 
string s, t;
 
int main() {
	ios :: sync_with_stdio(false), cin.tie(nullptr);
	cin >> n >> m >> s >> t;
	for (int i = 0; i < n; ++i) {
		for (int j = 1; j <= m; ++j)
			dp[i][j] = inf;
	}
	for (int i = 0; i < n; ++i) {
		if (s[i] == t[0])
			dp[i][1] = 0;
	}
	for (int j = 2; j <= m; ++j) {
		for (int i = 0; i < n; ++i) {
			for (int k = 0; k < n; ++k) {
				if (s[i] == s[k])
					dp[i][j - 1] = min(dp[i][j - 1], dp[k][j - 1] + abs(i - k)); 
			}
		}
		for (int i = 0; i < n; ++i) {
			if (s[i] == t[j - 1]) {
				if (i > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
				if (i + 1 < n) dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + 1);
			}
		}
	}
	ll ans = inf;
	for (int i = 0; i < n; ++i)
		ans = min(ans, dp[i][m]);
	if (ans == inf)
		ans = -1;
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 2284 KB Output is correct
2 Correct 53 ms 2288 KB Output is correct
3 Correct 45 ms 2412 KB Output is correct
4 Correct 48 ms 2412 KB Output is correct
5 Correct 4 ms 1644 KB Output is correct
6 Correct 4 ms 1644 KB Output is correct
7 Correct 68 ms 2284 KB Output is correct
8 Correct 68 ms 2284 KB Output is correct
9 Correct 101 ms 2284 KB Output is correct
10 Correct 4 ms 1516 KB Output is correct
11 Correct 69 ms 2412 KB Output is correct