zoukankan      html  css  js  c++  java
  • Atcoder F

    F - LCS


    Time Limit: 2 sec / Memory Limit: 1024 MB

    Score : 100100 points

    Problem Statement

    You are given strings ss and tt. Find one longest string that is a subsequence of both ss and tt.

    Notes

    subsequence of a string xx is the string obtained by removing zero or more characters from xx and concatenating the remaining characters without changing the order.

    Constraints

    • ss and tt are strings consisting of lowercase English letters.
    • 1|s|,|t|30001≤|s|,|t|≤3000

    Input

    Input is given from Standard Input in the following format:

    ss
    tt
    

    Output

    Print one longest string that is a subsequence of both ss and tt. If there are multiple such strings, any of them will be accepted.


    Sample Input 1 Copy

    Copy
    axyb
    abyxb
    

    Sample Output 1 Copy

    Copy
    axb
    

    The answer is axb or ayb; either will be accepted.


    Sample Input 2 Copy

    Copy
    aa
    xayaz
    

    Sample Output 2 Copy

    Copy
    aa
    

    Sample Input 3 Copy

    Copy
    a
    z
    

    Sample Output 3 Copy

    Copy
    
    

    The answer is  (an empty string).


    Sample Input 4 Copy

    Copy
    abracadabra
    avadakedavra
    

    Sample Output 4 Copy

    Copy
    aaadara


    题意:给定两个字符串s和t,让你求出这两个字符串的最长公共子序列,并输出最长公共子序列。
    思路:先通过DP求出LCS的DP信息,然后再根据DP信息输出对应的字符。
    裸题主要看思路。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    using namespace std;
    typedef long long ll;
    inline void getInt(int* p);
    const int maxn=1000010;
    const int inf=0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    int dp[3050][3050];
    char a[maxn];
    char b[maxn];
    int n,m;
    int c[maxn];
    int pre[maxn];
    int lis[maxn];
    int main()
    {
        scanf("%s",a);
        scanf("%s",b);
        n=strlen(a);
        m=strlen(b);
        for(int i=n-1;i>=0;i--)
        {
            for(int j=m-1;j>=0;j--)
            {
                if(a[i]==b[j])
                {
                    dp[i][j]=dp[i+1][j+1]+1;
                }else
                {
                    dp[i][j]=max(dp[i+1][j],dp[i][j+1]);
                }
            }
        }
    //    cout<<dp[n-1][m-1]<<endl;
        int i=0;
        int j=0;
        while(i<n&&j<m)
        {
            if(a[i]==b[j])
            {
                putchar(a[i]);
                i++;
                j++;
            }else if(dp[i][j]==dp[i+1][j])
            {
                i++;
            }else
            {
                j++;
            }
        }
    
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    lintcode:最大子正方形
    lintcode 中等题:k Sum ii k数和 II
    lintcode 中等题:A + B Problem A + B 问题
    Protege汉字不能正常显示问题
    Protege A DOT error has occurred错误
    lintcode :reverse integer 颠倒整数
    Reported time is too far out of sync with master. Time difference of 52692ms > max allowed of 30000ms
    Please add or free up more resources then turn off safe mode manually.
    Permission denied: user=root, access=WRITE, inode="/":hadoopuser:supergroup:drwxr-xr-x
    Hadoop重新格式化HDFS的方法
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/10276413.html
Copyright © 2011-2022 走看看