Submission #464373

# Submission time Handle Problem Language Result Execution time Memory
464373 2021-08-13T05:57:06 Z abc864197532 Bajka (COCI20_bajka) C++17
70 / 70
111 ms 676 KB
/*
 *   ##   #####   ####      ####  #    #  ####
 *  #  #  #    # #    #    #    # #    # #    #
 * #    # #####  #         #    # #    # #    #
 * ###### #    # #         #    # # ## # #    #
 * #    # #    # #    #    #    # ##  ## #    #
 * #    # #####   ####      ####  #    #  ####
 */
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair <int, int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define info() cerr << __PRETTY_FUNCTION__ << ": " << __LINE__ << endl
void abc() {cerr << endl;}
template <typename T, typename ...U> void abc(T a, U ...b) {
    cerr << a << ' ', abc(b...);
}
template <typename T> void printv(T l, T r) {
    while (l != r) cout << *l << " \n"[++l == r];
}
template <typename A, typename B> istream& operator >> (istream& o, pair<A, B> &a) {
    return o >> a.X >> a.Y;
}
template <typename A, typename B> ostream& operator << (ostream& o, pair<A, B> a) {
    return o << '(' << a.X << ", " << a.Y << ')';
}
template <typename T> ostream& operator << (ostream& o, vector<T> a) {
    bool is = false;
    for (T i : a) {o << (is ? ' ' : '{'), is = true, o << i;}
    return o << '}';
}
template <typename T> struct vv : vector <vector <T>> {
    vv(int n, int m, T v) : vector <vector <T>> (n, vector <T>(m, v)) {}
    vv() {}
};
template <typename T> struct vvv : vector <vv <T>> {
    vvv(int n, int m, int k, T v) : vector <vv <T>> (n, vv <T>(m, k, v)) {}
    vvv() {}
};
#ifdef Doludu
#define test(args...) info(), abc("[" + string(#args) + "]", args)
#define owo void(0)
#else
#define test(args...) void(0)
#define owo ios::sync_with_stdio(false); cin.tie(0)
#endif
const int mod = 1e9 + 7, N = 200001, logN = 18, K = 1000;

int main () {
    owo;
    int n, m;
    cin >> n >> m;
    vv <int> dp(n, m, 1 << 30);
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < n; ++i) if (s[i] == t[0]) dp[i][0] = 0;
    for (int i = 1; i < m; ++i) for (int j = 0; j < n; ++j) if (s[j] == t[i]) {
        if (j + 1 < n) {
            for (int k = 0; k < n; ++k) if (s[j + 1] == s[k]) {
                dp[j][i] = min(dp[j][i], dp[k][i - 1] + abs(k - j - 1) + 1);
            }
        }
        if (j) {
            for (int k = 0; k < n; ++k) if (s[j - 1] == s[k]) {
                dp[j][i] = min(dp[j][i], dp[k][i - 1] + abs(k - j + 1) + 1);
            }
        }
    }
    int ans = 1 << 30;
    for (int i = 0; i < n; ++i) ans = min(ans, dp[i][m - 1]);
    cout << (ans == 1 << 30 ? -1 : ans);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 676 KB Output is correct
2 Correct 13 ms 672 KB Output is correct
3 Correct 11 ms 588 KB Output is correct
4 Correct 9 ms 588 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 43 ms 588 KB Output is correct
8 Correct 73 ms 656 KB Output is correct
9 Correct 111 ms 588 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 40 ms 660 KB Output is correct