53542023-04-26 11:13:53sztomiRegexcpp11Accepted 100/100569ms103328 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";
    }
*/
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted43ms99620 KiB
2Accepted43ms99940 KiB
subtask29/9
3Accepted120ms100792 KiB
4Accepted400ms101016 KiB
5Accepted400ms101156 KiB
6Accepted400ms101332 KiB
7Accepted540ms101268 KiB
8Accepted481ms101516 KiB
subtask311/11
9Accepted43ms101140 KiB
10Accepted67ms101472 KiB
11Accepted64ms101680 KiB
12Accepted64ms101640 KiB
13Accepted64ms101632 KiB
14Accepted97ms101908 KiB
subtask413/13
15Accepted43ms101856 KiB
16Accepted97ms101864 KiB
17Accepted67ms101856 KiB
18Accepted97ms101920 KiB
19Accepted67ms101920 KiB
20Accepted65ms101920 KiB
subtask524/24
21Accepted45ms102088 KiB
22Accepted101ms102304 KiB
23Accepted68ms102456 KiB
24Accepted68ms102396 KiB
25Accepted68ms102572 KiB
26Accepted70ms102572 KiB
subtask643/43
27Accepted134ms103136 KiB
28Accepted476ms103052 KiB
29Accepted486ms103220 KiB
30Accepted560ms103276 KiB
31Accepted337ms103028 KiB
32Accepted446ms103224 KiB
33Accepted569ms103164 KiB
34Accepted492ms103328 KiB