53092023-04-25 19:01:23gortomiRegexcpp17Accepted 100/100189ms82060 KiB
#include <bits/stdc++.h>
using namespace std;
void solve()
{
    string a, b;
    cin >> a >> b;
    int n = a.size(), m = b.size();
    vector<vector<int> > dp1(n + 1, vector<int>(m + 1)), dp2(n + 1, vector<int>(m + 1, INT_MAX));
    for(int i = 0; i <= n; i++)
    {
        dp1[i][m] = n - i + 3;
        dp2[i][m] = n - i;
    }
    for(int i = 0; i <= m; i++)
    {
        dp1[n][i] = m - i + 3;
        dp2[n][i] = m - i;
    }
    dp1[n][m] = 0;
    for(int i = n - 1; i >= 0; i--)
    {
        for(int j = m - 1; j >= 0; j--)
        {
            if(a[i] == b[j])
            {
                dp1[i][j] = dp1[i + 1][j + 1] + 1;
                dp2[i][j] = dp1[i + 1][j + 1] + 1;
            }
            else dp1[i][j] = min(dp2[i + 1][j] + 4, dp2[i][j + 1] + 4);
            dp2[i][j] = min(dp2[i][j], dp2[i + 1][j] + 1);
            dp2[i][j] = min(dp2[i][j], dp2[i][j + 1] + 1);
        }
    }
    cout << dp1[0][0] << "\n";
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--) solve();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1828 KiB
2Accepted3ms2184 KiB
subtask29/9
3Accepted30ms50200 KiB
4Accepted122ms60596 KiB
5Accepted142ms70856 KiB
6Accepted122ms49432 KiB
7Accepted189ms71728 KiB
8Accepted165ms68732 KiB
subtask311/11
9Accepted3ms3204 KiB
10Accepted3ms3284 KiB
11Accepted3ms3644 KiB
12Accepted3ms3532 KiB
13Accepted3ms3720 KiB
14Accepted3ms3852 KiB
subtask413/13
15Accepted2ms3916 KiB
16Accepted3ms3932 KiB
17Accepted3ms4068 KiB
18Accepted3ms3984 KiB
19Accepted3ms3972 KiB
20Accepted3ms3996 KiB
subtask524/24
21Accepted3ms4624 KiB
22Accepted3ms4820 KiB
23Accepted4ms4664 KiB
24Accepted3ms4476 KiB
25Accepted3ms4432 KiB
26Accepted4ms4732 KiB
subtask643/43
27Accepted35ms57624 KiB
28Accepted134ms64076 KiB
29Accepted137ms79444 KiB
30Accepted165ms82060 KiB
31Accepted85ms37604 KiB
32Accepted123ms61096 KiB
33Accepted168ms69988 KiB
34Accepted153ms81856 KiB