Submission #366402

# Submission time Handle Problem Language Result Execution time Memory
366402 2021-02-14T06:41:07 Z NONAME Bajka (COCI20_bajka) C++14
70 / 70
39 ms 876 KB
#include <bits/stdc++.h>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
using namespace std;

const int man = (int)(3e2 + 10);
const int mam = (int)(3e2 + 10);

int n, m;
string s, t;
int dp[man][mam];

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #ifdef _LOCAL
        in("inB.txt");
        out("outB.txt");
    #endif

    cin >> n >> m;
    cin >> s;
    cin >> t;

    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            dp[i][j] = 1e9;
        }
    }

    for (int i = 1; i <= n; ++i) {
        if (s[i - 1] == t[0]) {
            dp[1][i] = 0;
        }
    }

    for (int i = 1; i < m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (s[j - 1] != t[i - 1]) {
                continue;
            }
            for (int k = 1; k <= n; ++k) {
                if (s[k - 1] != t[i - 1]) {
                    continue;
                }

                if ((k - 1) >= 1) {
                    if (s[k - 2] == t[i]) {
                        dp[i + 1][k - 1] = min(dp[i + 1][k - 1], dp[i][j] + abs(j - k) + 1);
                    }
                }
                if ((k + 1) <= n) {
                    if (s[k] == t[i]) {
                        dp[i + 1][k + 1] = min(dp[i + 1][k + 1], dp[i][j] + abs(j - k) + 1);
                    }
                }
            }
        }
    }

    int ans = 1e9;
    for (int i = 1; i <= n; ++i) {
        if (s[i - 1] != t.back()) {
            continue;
        }
        ans = min(ans, dp[m][i]);
    }
    if (ans == 1e9) {
        cout << "-1\n";
    } else {
        cout << ans << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 748 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 748 KB Output is correct
5 Correct 1 ms 748 KB Output is correct
6 Correct 1 ms 748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 796 KB Output is correct
2 Correct 4 ms 748 KB Output is correct
3 Correct 3 ms 748 KB Output is correct
4 Correct 3 ms 748 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 10 ms 748 KB Output is correct
8 Correct 29 ms 748 KB Output is correct
9 Correct 39 ms 876 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 9 ms 748 KB Output is correct