hdu4681--String

http://acm.hdu.edu.cn/showproblem.php?pid=4681

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".


                             D是A的子序列,也是B的子序列,C是D的子串

先找出A和B的最长公共子序列,然后再把A和B倒过来,求其最长公共子序列。最后把C的头和尾分别在A和B中标记下来,然后求之。

代码实现:

#include<stdio.h>
#include <iostream>
#include<string.h>
#define M 1007
using namespace std;
int dp1[M][M],dp2[M][M];
char s1[M],s2[M],s3[M],st1[M],st2[M];
int z1[M][2],z2[M][2];
int len1,len2,len3;
int main()
{
    int t;
    cin>>t;
    for(int cas=1; cas<=t; cas++)
    {
        cin>>s1>>s2>>s3;
        len1=strlen(s1);
        len2=strlen(s2);
        len3=strlen(s3);
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));

        //dp1[][]求的是s1和s2的最大公共子序列
        for(int i=1; i<=len1; i++)
            for(int j=1; j<=len2; j++)
                if(s1[i-1]==s2[j-1])
                    dp1[i][j]=dp1[i-1][j-1]+1;
                else
                    dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1]);

        //st1是将s1字符串转过来
        for(int i=0; i<len1; i++)
            st1[i]=s1[len1-i-1];

        //st2是将s2字符串转过来
        for(int i=0; i<len2; i++)
            st2[i]=s2[len2-i-1];

        //求的是st1和st2的最长公共子序列
        for(int i=1; i<=len1; i++)
            for(int j=1; j<=len2; j++)
                if(st1[i-1]==st2[j-1])
                    dp2[i][j]=dp2[i-1][j-1]+1;
                else
                    dp2[i][j]=max(dp2[i-1][j],dp2[i][j-1]);


        //将s3的起点和终点在s1中的所有情况记录下来
        int n1=0,n2=0,k,j;
        for(int i=0; i<len1; i++)
        {
            if(s1[i]==s3[0])
            {
                k=1;
                for(j=i+1; j<len1; j++)
                {
                    if(s1[j]==s3[k])
                        k++;
                    if(k==len3)
                        break;
                }
                if(j!=len1)
                {
                    z1[n1][0]=i;
                    z1[n1++][1]=j;
                }
            }
        }

        //将s3的起点和终点在s2中的所有情况记录下来
        for(int i=0; i<len2; i++)
        {
            if(s2[i]==s3[0])
            {
                k=1;
                for(j=i+1; j<len2; j++)
                {
                    if(s2[j]==s3[k])
                        k++;
                    if(k==len3)
                        break;
                }
                if(j!=len2)
                {
                    z2[n2][0]=i;
                    z2[n2++][1]=j;
                }
            }
        }

        //枚举所有情况,找出s4最大的长度
        int ans=0;
        for(int i=0; i<n1; i++)
            for(int j=0; j<n2; j++)
                ans=max(ans,dp1[z1[i][0]][z2[j][0]]+dp2[len1-z1[i][1]-1][len2-z2[j][1]-1]);//应为从0开始的所以要减去1

        cout<<"Case #"<<cas<<": "<<ans+len3<<endl;
        }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值