Submission #1183208

#TimeUsernameProblemLanguageResultExecution timeMemory
1183208nagorn_phBajka (COCI20_bajka)C++20
20 / 70
4 ms1092 KiB
#include <bits/stdc++.h> #define int long long #define double long double #define pii pair <int,int> #define tiii tuple <int, int, int> #define f first #define s second #define all(x) x.begin(), x.end() #define ub(a, b) upper_bound(a.begin(), a.end(), b) - a.begin() #define lb(a, b) lower_bound(a.begin(), a.end(), b) - a.begin() #define ve vector #define graph(a, n) vector <int> a[n]; #define wgraph(a, n) vector <pii> a[n]; #define emb emplace_back #define em emplace #define ins insert #define er erase #define iShowSpeed cin.tie(NULL)->sync_with_stdio(false) using namespace std; template <typename T> using greater_priority_queue = priority_queue<T, vector<T>, greater<T>>; const int mod = 1e9 + 7; const int inf = 1e18; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int32_t main(){ iShowSpeed; int n, m; cin >> n >> m; string aa, bb; cin >> aa >> bb; vector <int> a, b; for (auto c : aa) a.emplace_back((int)(c - 'a')); for (auto c : bb) b.emplace_back((int)(c - 'a')); vector <vector <int>> dp(m + 1, vector <int> (n + 1, inf)); // dp[i][j] = min time to reach position i and end at position j for (int i = 0; i <= n; i++) dp[0][i] = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (a[j - 1] != b[i - 1]) continue; if (j > 1 && a[j - 1] == b[i - 1]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1); if (j < n && a[j + 1] == b[i - 1]) dp[i][j] = min(dp[i][j], dp[i - 1][j + 1] + 1); for (int k = 1; k <= n; k++) { if (j == k && i > 1) continue; if (i == 1 || a[k - 1] == b[i - 2]) { dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(j - k)); } } } } // for (int i = 1; i <= m; i++) { // for (int j = 1; j <= n; j++) { // if (dp[i][j] == inf) cout << "X "; // else cout << dp[i][j] << " "; // } // cout << "\n"; // } int ans = *min_element(all(dp[m])); cout << (ans == inf ? -1 : ans); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...