hdu 4681 String(dp)

本文介绍了一种解决最长公共子序列问题的算法,通过寻找两个字符串A和B中最长的公共子序列,同时确保第三个字符串C是该序列的连续子串。文章详细描述了算法流程,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
Given 3 strings A, B, C, find the longest string D which satisfy the following rules: a) D is the subsequence of A b) D is the subsequence of B c) C is the substring of D Substring here means a consecutive subsequnce. You need to output the length of D.
 

Input
The first line of the input contains an integer T(T = 20) which means the number of test cases. For each test case, the first line only contains string A, the second line only contains string B, and the third only contains string C. The length of each string will not exceed 1000, and string C should always be the subsequence of string A and string B. All the letters in each string are in lowercase.
 

Output
For each test case, output Case #a: b. Here a means the number of case, and b means the length of D.
 

Sample Input
2 aaaaa aaaa aa abcdef acebdf cf
 

Sample Output
Case #1: 4 Case #2: 3
Hint
For test one, D is "aaaa", and for test two, D is "acf".
左右分别跑一遍最长公共子序列,再枚举在A,B中C的位置,然后三段求和即可。复杂度O(n^2)。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
const int maxn = 1000 + 5;
const int INF = 2000000000;
typedef  pair<int, int> P;
typedef long long LL;

char a[maxn],b[maxn],c[maxn];
int dpl[maxn][maxn],dpr[maxn][maxn];
int n,m;
void pre(){
    n = strlen(a+1);
    m = strlen(b+1);
    memset(dpl,0,sizeof(dpl));
    memset(dpr,0,sizeof(dpr));
    for(int i = 0;i <= m;i++){
        dpl[0][i] = 0;
    }
    for(int i = 0;i <= n;i++){
        dpl[i][0] = 0;
    }
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            if(a[i] == b[j]) dpl[i][j] = dpl[i-1][j-1]+1;
            else dpl[i][j] = max(dpl[i-1][j],dpl[i][j-1]);
        }
    }
    for(int i = 0;i <= m;i++){
        dpr[n+1][i] = 0;
    }
    for(int i = 0;i <= n;i++){
        dpr[i][m+1] = 0;
    }
    for(int i = n;i >= 1;i--){
        for(int j = m;j >= 1;j--){
            if(a[i] == b[j]) dpr[i][j] = dpr[i+1][j+1]+1;
            else dpr[i][j] = max(dpr[i+1][j],dpr[i][j+1]);
        }
    }
}

int la[maxn],lb[maxn],ra[maxn],rb[maxn];

int main(){
    int t,kase = 0;
    scanf("%d",&t);
    while(t--){
        kase++;
        scanf("%s",a+1);
        scanf("%s",b+1);
        scanf("%s",c+1);
        pre();
        int len = strlen(c+1);
        int p = 1,cnta = 0;
        while(p <= n){
            int fir = -1;
            int i,j;
            for(i = 1,j = p;i <= len && j <= n;){
                if(c[i] == a[j]){
                    if(fir == -1) fir = j;
                    i++;
                }
                j++;
            }
            p++;
            if(i == len+1){
               la[cnta] = fir;
               ra[cnta++] = j-1;
            }
        }
        p = 1;
        int cntb = 0;
        while(p <= m){
            int fir = -1;
            int i,j;
            for(i = 1,j = p;i <= len && j <= m;){
                if(c[i] == b[j]){
                    if(fir == -1) fir = j;
                    i++;
                }
                j++;
            }
            p++;
            if(i == len+1){
               lb[cntb] = fir;
               rb[cntb++] = j-1;
            }
        }
        int ans = 0;
        for(int i = 0;i < cnta;i++){
            for(int j = 0;j < cntb;j++){
                ans = max(ans,len+dpl[la[i]-1][lb[j]-1]+dpr[ra[i]+1][rb[j]+1]);
            }
        }
        printf("Case #%d: %d\n",kase,ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值