Submission #938191

#TimeUsernameProblemLanguageResultExecution timeMemory
938191weakweakweakBajka (COCI20_bajka)C++17
50 / 70
34 ms1476 KiB
#include <bits/stdc++.h> using namespace std; int n, m, dp[510][510] = {0}; string s, t; int main () { memset(dp, 63, sizeof(dp)); ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> s >> t; t = "%" + t; for (int i = 0; i < n; i++) dp[0][i] = -1; for (int i = 1; i <= m; i++) { for (int j = 0; j < n; j++) { if (s[j] != t[i]) continue; if (j > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1); if (j + 1 < n) dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + 1); } for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) if (s[j] == s[k]) dp[i][j] = min(dp[i][j], dp[i][k] + abs(j - k)); } int ans = INT_MAX; for (int i = 0; i < n; i++) ans = min(ans, dp[m][i]); if (ans > 100000) ans = -1; cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...