답안 #478034

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
478034 2021-10-05T06:47:04 Z chungdinh Bajka (COCI20_bajka) C++14
70 / 70
65 ms 1484 KB
#include <iostream>
#include <vector>
#include <bitset>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stack>
#include <set>
#include <map>
#include <cassert>
#include <bits/stdc++.h>

using namespace std;

#define pb push_back

#define ll long long
#define ii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define r first
#define c second

#define all(x) x.begin(), x.end()

ostream& operator << (ostream& os, pair<int, int> a) {
    return os << a.first << " : " << a.second;
}

#define endl '\n'
#define db(val) "["#val" = "<<(val)<<"] "
#define cntbit(x) __builtin_popcount(x)

const int N = 500 + 5;
const int iINF = 1e9;
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1e18;

int n, m;
string s, t;

ll dp[N][N];

int main() {
    #ifdef CHUNGDINH
    freopen("main.inp","r",stdin);
    //freopen("main.out","w",stdout);
    #endif

    cin >> n >> m >> s >> t;
    s = '#' + s, t = '#' + t;

    if (m == 1) {
        if (s == t) cout << 0; else cout << -1;
        return 0;
    }

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

    ll res = INF;

    for (int k = 2; k <= m; k++) {
        for (int i = 1; i <= n; i++) {
            dp[i][k] = INF;
            if (s[i] != t[k]) continue;
            for (int j = 1; j <= n; j++) {
                if (s[j] != t[k - 1]) continue;
                if (j >= 2 && s[j - 1] == s[i]) {
                    dp[i][k] = min(dp[i][k], dp[j][k - 1] + abs(i - (j - 1)) + 1);
                }
                if (j <= n - 1 && s[j + 1] == s[i]) {
                    dp[i][k] = min(dp[i][k], dp[j][k - 1] + abs(i - (j + 1)) + 1);
                }
            }
            if (k == m) res = min(res, dp[i][k]);
        }
    }
    if (res >= INF) res = -1;
    cout << res;
}

/*
Array bounds *
long long vs int
Garbage value
Sometimes, VNOI views "arrays out of bounds" as "wrong answer"
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1484 KB Output is correct
2 Correct 4 ms 1484 KB Output is correct
3 Correct 3 ms 1484 KB Output is correct
4 Correct 3 ms 1484 KB Output is correct
5 Correct 1 ms 1484 KB Output is correct
6 Correct 1 ms 1484 KB Output is correct
7 Correct 11 ms 1484 KB Output is correct
8 Correct 31 ms 1356 KB Output is correct
9 Correct 65 ms 1356 KB Output is correct
10 Correct 1 ms 1356 KB Output is correct
11 Correct 8 ms 1484 KB Output is correct