Submission #758707

#TimeUsernameProblemLanguageResultExecution timeMemory
758707JANCARAPANBajka (COCI20_bajka)C++17
70 / 70
45 ms980 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define sz(a) (long long) a.size() #define endl '\n' const long long INF = 1e18, MOD = 1e9+7; void test_case() { int n, m; string s, t; cin >> n >> m >> s >> t; vector dp(m + 1, vector<ll>(n, INF)); for (int cur_p = 0; cur_p < n; cur_p++) { if (s[cur_p] == t[0]) dp[0][cur_p] = 0; } for (int i = 0; i < m - 1; i++) { for (int cur_p = 0; cur_p < n; cur_p++) { if (s[cur_p] == t[i]) { for (int new_p = 0; new_p < n; new_p++) { if (s[new_p] == t[i + 1] and new_p > 0 and s[new_p - 1] == t[i]) { dp[i + 1][new_p] = min(dp[i + 1][new_p], dp[i][cur_p] + abs(cur_p - (new_p - 1)) + 1); } if (s[new_p] == t[i + 1] and new_p + 1 < n and s[new_p + 1] == t[i]) { dp[i + 1][new_p] = min(dp[i + 1][new_p], dp[i][cur_p] + abs(cur_p - (new_p + 1)) + 1); } } } } } ll ans = INF; for (int cur_p = 0; cur_p < n; cur_p++) { ans = min(ans, dp[m - 1][cur_p]); } cout << (ans == INF ? -1 : ans) << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int tt = 1; //cin >> tt; while (tt--) { test_case(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...