Submission #320886

# Submission time Handle Problem Language Result Execution time Memory
320886 2020-11-10T06:57:36 Z dolphingarlic Bajka (COCI20_bajka) C++14
70 / 70
26 ms 364 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int dp[2][300];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    string s, t;
    vector<int> pos;
    cin >> n >> m >> s >> t;
    for (int i = 0; i < n; i++) if (s[i] == t[0]) pos.push_back(i);
    for (int i = 1; i < m; i++) {
        pos.clear();
        for (int j = 0; j < n; j++) if (s[j] == t[i]) {
            pos.push_back(j);
            dp[i & 1][j] = INT_MAX / 2;
            if (j && s[j - 1] == t[i - 1]) {
                dp[i & 1][j] = min(dp[i & 1][j], dp[i - 1 & 1][j - 1] + 1);
            }
            if (j < n - 1 && s[j + 1] == t[i - 1]) {
                dp[i & 1][j] = min(dp[i & 1][j], dp[i - 1 & 1][j + 1] + 1);
            }
        }
        for (int j : pos) for (int k : pos) dp[i & 1][j] = min(dp[i & 1][j], dp[i & 1][k] + abs(k - j));
    }

    int ans = INT_MAX / 2;
    for (int i : pos) ans = min(ans, dp[m - 1 & 1][i]);
    if (ans == INT_MAX / 2) cout << -1;
    else cout << ans;
    return 0;
}

Compilation message

bajka.cpp: In function 'int main()':
bajka.cpp:21:55: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |                 dp[i & 1][j] = min(dp[i & 1][j], dp[i - 1 & 1][j - 1] + 1);
      |                                                     ~~^~~
bajka.cpp:24:55: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   24 |                 dp[i & 1][j] = min(dp[i & 1][j], dp[i - 1 & 1][j + 1] + 1);
      |                                                     ~~^~~
bajka.cpp:31:43: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   31 |     for (int i : pos) ans = min(ans, dp[m - 1 & 1][i]);
      |                                         ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 0 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 364 KB Output is correct
2 Correct 2 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 0 ms 364 KB Output is correct
6 Correct 0 ms 364 KB Output is correct
7 Correct 5 ms 364 KB Output is correct
8 Correct 13 ms 364 KB Output is correct
9 Correct 26 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 3 ms 364 KB Output is correct