40542023-03-11 10:55:43zsomborRegexcpp17Accepted 100/100122ms145976 KiB
#include <iostream>
#include <vector>
using namespace std;

string a, b;
int n, m;
vector <vector <int>> dp(3e3, vector <int>(3e3, 1e9));
vector <vector <int>> mn(3e3, vector <int>(3e3, 0));

void solve() {
    cin >> a >> b;
    n = a.size();
    m = b.size();
    a = "a" + a;
    b = "a" + b;
    dp[0][0] = n + m + 3;
    for (int i = 0; i <= n; i++) mn[i][0] = dp[0][0];
    for (int j = 0; j <= m; j++) mn[0][j] = dp[0][0];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            mn[i][j] = min(mn[i - 1][j], mn[i][j - 1]);
            if (a[i] != b[j]) continue;
            dp[i][j] = mn[i - 1][j - 1] + 2;
            if (a[i - 1] == b[j - 1]) dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] - 1);
            if (i == n && j == m) dp[i][j] -= 3;
            mn[i][j] = min(mn[i][j], dp[i][j]);
        }
    }
    cout << mn[n][m] << endl;
}

int main()
{
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) solve();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted50ms142892 KiB
2Accepted48ms143052 KiB
subtask29/9
3Accepted71ms143404 KiB
4Accepted90ms143740 KiB
5Accepted104ms143876 KiB
6Accepted92ms143928 KiB
7Accepted104ms144248 KiB
8Accepted111ms144456 KiB
subtask311/11
9Accepted50ms144404 KiB
10Accepted48ms144420 KiB
11Accepted61ms144532 KiB
12Accepted61ms144548 KiB
13Accepted61ms144800 KiB
14Accepted61ms144876 KiB
subtask413/13
15Accepted50ms144956 KiB
16Accepted48ms144876 KiB
17Accepted61ms144776 KiB
18Accepted48ms144780 KiB
19Accepted61ms145108 KiB
20Accepted63ms145172 KiB
subtask524/24
21Accepted63ms145052 KiB
22Accepted63ms145260 KiB
23Accepted63ms145264 KiB
24Accepted63ms145348 KiB
25Accepted63ms145484 KiB
26Accepted63ms145412 KiB
subtask643/43
27Accepted61ms145668 KiB
28Accepted112ms145660 KiB
29Accepted111ms145652 KiB
30Accepted119ms145636 KiB
31Accepted79ms145828 KiB
32Accepted104ms145976 KiB
33Accepted122ms145836 KiB
34Accepted103ms145836 KiB