hdu 4681 String(暴力&dp&LCS)

String

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


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

Source
2013 Multi-University Training Contest 8
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   4790  4789  4788  4787  4786 
 
题意:
给你A,B,C三个字符串。叫你找出一个最长的D串。满足D串是A,B串的子序列。(不用连续)。而C串是D串的子串。(必须连续)。要你求出D串的最大长度。串长都不超过1000。
思路:
开始把题意理解错了。认为D是A,B的子串。到最后都没纠结出来。明白题意后就简单了。只需要做两次LCS一个从前。一个从后。的到fdp[i][j]表示。A的前i和B的前j的最长公共子序列。ldp[i][j]表示后i,后j的最长公共子序列。剩下的工作就是找出C串可以在A,B串中出现的位置了。然后C串长度+前后最长公共子序列的长度就是答案了。
详细见代码:
#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=100010;
typedef __int64 ll;
int fdp[1010][1010],ldp[1010][1010];
char A[1010],B[1010],C[1010];
int na,nb;
struct node
{
    int x,y;
} pa[1010],pb[1010];
int main()
{
    int t,cas=1,i,j,k,la,lb,lc,ans;

    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s%s",A+1,B+1,C+1);
        la=strlen(A+1);
        lb=strlen(B+1);
        lc=strlen(C+1);
        memset(fdp,0,sizeof fdp);
        memset(ldp,0,sizeof ldp);//被初始化坑了。
        for(i=1;i<=la;i++)
        {
            for(j=1;j<=lb;j++)
            {
                if(A[i]==B[j])
                    fdp[i][j]=fdp[i-1][j-1]+1;
                else
                    fdp[i][j]=max(fdp[i][j-1],fdp[i-1][j]);
            }
        }
        for(i=la;i>=1;i--)
        {
            for(j=lb;j>=1;j--)
            {
                if(A[i]==B[j])
                    ldp[i][j]=ldp[i+1][j+1]+1;
                else
                    ldp[i][j]=max(ldp[i][j+1],ldp[i+1][j]);
            }
        }
        na=nb=0;
        for(i=1;i<=la;i++)
        {
            if(C[1]==A[i])
            {
                for(k=i+1,j=2;k<=la;k++)
                {
                    if(C[j]==A[k])
                        j++;
                    if(j==lc+1)//先后次序有关系。因为k不同了。
                        break;//不用往后找了。因为终点越早产生的值越大。
                }
                if(j==lc+1)
                {
                    pa[na].x=i;
                    pa[na].y=k;
                    na++;
                }
                else
                    break;//后面串凑不齐了。不用再找了。
            }
        }
        for(i=1;i<=lb;i++)
        {
            if(C[1]==B[i])
            {
                for(k=i+1,j=2;k<=lb;k++)
                {
                    if(C[j]==B[k])
                        j++;
                    if(j==lc+1)
                        break;
                }
                if(j==lc+1)
                {
                    pb[nb].x=i;
                    pb[nb].y=k;
                    nb++;
                }
                else
                    break;//后面串凑不齐了。不用再找了。
            }
        }
        ans=0;
        for(i=0;i<na;i++)
            for(j=0;j<nb;j++)
                ans=max(ans,fdp[pa[i].x-1][pb[j].x-1]+ldp[pa[i].y+1][pb[j].y+1]);
        printf("Case #%d: %d\n",cas++,ans+lc);
    }
    return 0;
}


你可能感兴趣的:(c,算法,ACM)