Submission #758707

# Submission time Handle Problem Language Result Execution time Memory
758707 2023-06-15T07:30:30 Z JANCARAPAN Bajka (COCI20_bajka) C++17
70 / 70
45 ms 980 KB
#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 time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 980 KB Output is correct
2 Correct 4 ms 980 KB Output is correct
3 Correct 3 ms 964 KB Output is correct
4 Correct 4 ms 980 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 12 ms 980 KB Output is correct
8 Correct 32 ms 980 KB Output is correct
9 Correct 45 ms 980 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 9 ms 960 KB Output is correct