5354 2023. 04. 26 11:13:53 sztomi Regex cpp11 Accepted 100/100 569ms 103328 KiB
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e5;
const int MAXN = 2500;

string a, b;

int dp[MAXN][MAXN][2];

int megold(int a_ind, int b_ind, bool nyitott){
    if(a_ind == a.size()){
        if(b_ind == b.size()){
            return 0;
        }
        else{
            return (b.size() - b_ind) + 3*(!nyitott);
        }
    }
    if(b_ind == b.size()){
        return (a.size() - a_ind) + 3*(!nyitott);
    }

    if(dp[a_ind][b_ind][nyitott] != -1){
        return dp[a_ind][b_ind][nyitott];
    }

    int ret = INF;
    if(nyitott){
        ret = min(ret, megold(a_ind+1, b_ind, true) + 1);
        ret = min(ret, megold(a_ind, b_ind+1, true) + 1);

        if(a[a_ind] == b[b_ind]){
            ret = min(ret, megold(a_ind+1, b_ind+1, false) + 1);
        }
    }
    else{
        ret = min(ret, megold(a_ind+1, b_ind, true) + 4);
        ret = min(ret, megold(a_ind, b_ind+1, true) + 4);

        if(a[a_ind] == b[b_ind]){
            ret = min(ret, megold(a_ind+1, b_ind+1, false) + 1);
        }
    }

    dp[a_ind][b_ind][nyitott] = ret;
    return ret;
}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    // dp proba?
    int t;
    cin >> t;
    while(t--){
        cin >> a >> b;
        memset(dp, -1, sizeof(dp));
        cout << megold(0, 0, false) << "\n";
    }

/*
    for(int k = 0; k < 2; k++){
        cout << "\t";
        for(int i = 0; i < b.size(); i++){
            cout << i << "\t";
        }
        cout << "\n";
        for(int i = 0; i < a.size(); i++){
            cout << i << "\t";
            for(int j = 0; j < b.size(); j++){
                cout << dp[i][j][k] << "\t";
            }
            cout << "\n";
        }
        cout << "----------------------\n";
    }
*/
}
Subtask Sum Test Verdict Time Memory
subtask1 0/0
1 Accepted 43ms 99620 KiB
2 Accepted 43ms 99940 KiB
subtask2 9/9
3 Accepted 120ms 100792 KiB
4 Accepted 400ms 101016 KiB
5 Accepted 400ms 101156 KiB
6 Accepted 400ms 101332 KiB
7 Accepted 540ms 101268 KiB
8 Accepted 481ms 101516 KiB
subtask3 11/11
9 Accepted 43ms 101140 KiB
10 Accepted 67ms 101472 KiB
11 Accepted 64ms 101680 KiB
12 Accepted 64ms 101640 KiB
13 Accepted 64ms 101632 KiB
14 Accepted 97ms 101908 KiB
subtask4 13/13
15 Accepted 43ms 101856 KiB
16 Accepted 97ms 101864 KiB
17 Accepted 67ms 101856 KiB
18 Accepted 97ms 101920 KiB
19 Accepted 67ms 101920 KiB
20 Accepted 65ms 101920 KiB
subtask5 24/24
21 Accepted 45ms 102088 KiB
22 Accepted 101ms 102304 KiB
23 Accepted 68ms 102456 KiB
24 Accepted 68ms 102396 KiB
25 Accepted 68ms 102572 KiB
26 Accepted 70ms 102572 KiB
subtask6 43/43
27 Accepted 134ms 103136 KiB
28 Accepted 476ms 103052 KiB
29 Accepted 486ms 103220 KiB
30 Accepted 560ms 103276 KiB
31 Accepted 337ms 103028 KiB
32 Accepted 446ms 103224 KiB
33 Accepted 569ms 103164 KiB
34 Accepted 492ms 103328 KiB