Xâu con chung dài nhất

View as PDF

Submit solution


Points: 0.06 (partial)
Time limit: 1.0s
Memory limit: 512M
Input: stdin
Output: stdout

Problem type
Allowed languages
C, C++, Go, Java, Kotlin, Pascal, PyPy, Python, Rust, Scratch

Xâu ký tự ~X~ được gọi là xâu con của xâu ký tự ~Y~ nếu ta có thể xoá đi một số ký tự trong xâu ~Y~ để được xâu ~X~.

Cho biết hai xâu ký tự ~A~ và ~B~ độ dài không quá ~2000~ ký tự, hãy tìm xâu ký tự ~C~ có độ dài lớn nhất và là con của cả ~A~ và ~B~.

Input

Dòng ~1~: chứa xâu ~A~

Dòng ~2~: chứa xâu ~B~

Output

Chỉ gồm một dòng ghi độ dài xâu ~C~ tìm được

Sample Input

abc1def2ghi3
abcdefghi123

Sample Output

10

Comments

Please read the guidelines before commenting.



  • 4
    hoangquys  commented on Nov. 22, 2025, 2:20 p.m.

    Ý Tưởng Giải


    1. Đặt dp[n][m] n là độ dài string A, m là độ dài string B

    2. Trường hợp cơ sở: dp[i][0] = 0; dp[0][j] = 0

    3. Công thức truy hồi: Nếu a[i] == b[j] thì dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

    4. Kết quả bài toán: dp[n][m]


  • 2
    hieuducle  commented on Aug. 17, 2025, 2:03 p.m.

    include<bits/stdc++.h>

    using namespace std; string x , y , dp[2005][2005]; string cmp (string a,string b) { while(a.size() < b.size()) a = '0' + a; while(b.size() < a.size()) b = '0' + b; if(a>b)return a; return b; } int main () { iosbase::syncwith_stdio(false); cin.tie(nullptr);

    cin >> x >> y;
    int m = x.size() , n = y.size();
    x = " " + x; 
    y = " " + y;
    dp[0][0] = '0';
    for(int i=1;i<=m;++i)
    {
        for(int j=1;j<=n;++j)
        {
            if(x[i] == y[j]) dp[i][j] = dp[i-1][j-1] + x[i];
            else dp[i][j] = cmp(dp[i-1][j] , dp[i][j-1]);
        }
    }
    string h = dp[m][n];
    while(h[0] == '0') h.erase(0,1);
    cout << h.size();
    return 0;
    

    }


  • -7
    zatarainbow  commented on Nov. 25, 2024, 1:17 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -9
    vdtue  commented on Aug. 20, 2024, 2:29 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • -14
    frekraiko  commented on Aug. 17, 2024, 2:09 a.m.

    This comment is hidden due to too much negative feedback. Show it anyway.


  • 24
    CBNK30_dinhson  commented on July 31, 2023, 3:36 a.m.

    ảo thật đấy, từ bé đến giờ


    • 15
      CBNK30_hoangphu  commented on July 31, 2023, 3:46 a.m.

      các bạn chú ý ngôn từ của bản thân đi


      • -83
        htmanh  commented on July 31, 2023, 3:47 a.m.

        This comment is hidden due to too much negative feedback. Show it anyway.


      • -36
        10tin_hangan  commented on July 31, 2023, 3:47 a.m.

        This comment is hidden due to too much negative feedback. Show it anyway.


    • -55
      htmanh  commented on July 31, 2023, 3:46 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.


    • -36
      10tin_hangan  commented on July 31, 2023, 3:42 a.m.

      This comment is hidden due to too much negative feedback. Show it anyway.