답안 #484924

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
484924 2021-11-05T18:05:59 Z Kirill22 Bajka (COCI20_bajka) C++17
70 / 70
56 ms 312 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define fi first
#define se second

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    string a, b;
    cin >> a >> b;
    vector<int> dp(n, (int) 1e18);
    for (int i = 0; i < n; i++) {
        if (a[i] == b[0]) {
            dp[i] = 0;
        }
    }
    for (int j = 1; j < m; j++) {
        vector<int> dp2(n, (int) 1e18);
        for (int i = 0; i < n; i++) {
            for (int pos = 0; pos < n; pos++) {
                if (a[pos] == b[j]) {
                    if (abs(i - pos) == 1) {
                        dp2[pos] = min(dp2[pos], dp[i] + 1);
                    }
                    if (pos && a[pos - 1] == a[i]) {
                        dp2[pos] = min(dp2[pos], dp[i] + abs(i - (pos - 1)) + 1);
                    }
                    if (pos + 1 < n && a[pos + 1] == a[i]) {
                        dp2[pos] = min(dp2[pos], dp[i] + abs(i - (pos + 1)) + 1);
                    }
                }
            }
        }
        swap(dp, dp2);
    }
    if (*min_element(all(dp)) == (int) 1e18) {
        cout << -1;
        return 0;
    }
    cout << *min_element(all(dp));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 204 KB Output is correct
2 Correct 21 ms 312 KB Output is correct
3 Correct 20 ms 204 KB Output is correct
4 Correct 25 ms 312 KB Output is correct
5 Correct 2 ms 204 KB Output is correct
6 Correct 2 ms 312 KB Output is correct
7 Correct 41 ms 204 KB Output is correct
8 Correct 54 ms 312 KB Output is correct
9 Correct 56 ms 308 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 40 ms 312 KB Output is correct