답안 #333876

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
333876 2020-12-08T01:31:47 Z wiwiho Bajka (COCI20_bajka) C++14
70 / 70
25 ms 1024 KB
#include <bits/stdc++.h>

#define pii pair<int, int>
#define mp make_pair
#define F first
#define S second
#define iter(a) a.begin(), a.end()

using namespace std;

typedef long long ll;

const ll MAX = 2147483647;

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

    int n, m;
    cin >> n >> m;

    string a, b;
    cin >> a >> b;
    vector<vector<ll>> dp(m, vector<ll>(n, MAX));
    for(int i = 0; i < n; i++){
        if(a[i] == b[0]) dp[0][i] = 0;
    }

    for(int i = 0; i < m - 1; i++){
        for(int j = 0; j < n; j++){
            if(dp[i][j] == MAX) continue;
            for(int k = 0; k < n; k++){
                if(a[k] == b[i]) dp[i][k] = min(dp[i][k], dp[i][j] + abs(j - k));
            }
        }
        for(int j = 0; j < n; j++){
            if(dp[i][j] == MAX) continue;
            if(j && a[j - 1] == b[i + 1]) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], dp[i][j] + 1);
            if(j < n - 1 && a[j + 1] == b[i + 1]) dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1);
        }
    }

    int ans = *min_element(iter(dp[m - 1]));
    if(ans == MAX) ans = -1;
    cout << ans << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 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 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1004 KB Output is correct
2 Correct 7 ms 1004 KB Output is correct
3 Correct 4 ms 1024 KB Output is correct
4 Correct 4 ms 1004 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 14 ms 1004 KB Output is correct
8 Correct 24 ms 1004 KB Output is correct
9 Correct 25 ms 1004 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 12 ms 1004 KB Output is correct