程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 4679 String

HDU 4679 String

編輯:C++入門知識

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

 


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
  
  
    祭奠一下這個題

#include <iostream>   
#include <string>   
#include <cstring>   
#include <cstdio>   
#define N 1100   
using namespace std;  
string s1,s2,s3,s4,s5;  
char temp[N];  
int num1[N][N],num2[N][N];  
struct num  
{  
    int sta,end;  
} a[N],b[N];  
int main()  
{  
    //freopen("data.in","r",stdin);   
    void get(int (*p)[N],string ch1,string ch2);  
    int t,tem=1;  
    scanf("%d",&t);  
    while(t--)  
    {  
        cin>>s1>>s2>>s3;  
        get(num1,s1,s2);  
        int l1 = s1.size();  
        for(int i=0; i<=l1-1; i++)  
        {  
            temp[l1-1-i] = s1[i];  
        }  
        temp[l1] = '\0';  
        s4 = temp;  
        int l2 = s2.size();  
        for(int i=0; i<=l2-1; i++)  
        {  
            temp[l2-1-i] = s2[i];  
        }  
        temp[l2] = '\0';  
        s5 = temp;  
        get(num2,s4,s5);  
        int l3 = s3.size(),Top1=0,Top2=0;  
        for(int i=0; i<=l1-1; i++)  
        {  
            if(s1[i]==s3[0])  
            {  
                int x = 0;  
                int sta = i;  
                int end = -1;  
                for(int j=i; j<=l1-1&&x<=l3-1; j++)  
                {  
                    if(s1[j]==s3[x])  
                    {  
                        x++;  
                    }  
                    if(x==l3)  
                    {  
                        end = j;  
                    }  
                }  
                if(end==-1)  
                {  
                    continue;  
                }  
                a[Top1].sta = sta;  
                a[Top1++].end = end;  
            }  
        }  
        for(int i=0; i<=l2-1; i++)  
        {  
            if(s2[i]==s3[0])  
            {  
                int x = 0;  
                int sta = i;  
                int end = -1;  
                for(int j=i; j<=l2-1&&x<=l3-1; j++)  
                {  
                    if(s2[j]==s3[x])  
                    {  
                        x++;  
                    }  
                    if(x==l3)  
                    {  
                        end = j;  
                    }  
                }  
                if(end==-1)  
                {  
                    continue;  
                }  
                b[Top2].sta = sta;  
                b[Top2++].end = end;  
            }  
        }  
        int res = l3,Max=0;  
        for(int i=0; i<=Top1-1; i++)  
        {  
            for(int j=0; j<=Top2-1; j++)  
            {  
                int x1 = a[i].sta;  
                int y1 = a[i].end;  
                int x2 = b[j].sta;  
                int y2 = b[j].end;  
                int k1 = 0;  
                if(x1>0&&x2>0)  
                {  
                    k1 = num1[x1-1][x2-1];  
                }  
                int k2 = 0;  
                if(y1<l1-1&&y2<l2-1)  
                {  
                    k2 = num2[l1-2-y1][l2-2-y2];  
                }  
                Max = max(Max,res+k2+k1);  
            }  
        }  
        printf("Case #%d: %d\n",tem++,Max);  
    }  
    return 0;  
}  
void get(int (*p)[N],string ch1,string ch2)  
{  
    int l1 = ch1.size();  
    int l2 = ch2.size();  
    memset(p,0,sizeof(p));  
    for(int i=0; i<=l1-1; i++)  
    {  
        for(int j=0; j<=l2-1; j++)  
        {  
            if(i==0&&j==0)  
            {  
                if(ch1[i]==ch2[j])  
                {  
                    p[i][j] = 1;  
                }  
            }  
            else if(i==0&&j!=0)  
            {  
                if(ch1[i]==ch2[j])  
                {  
                    p[i][j] = 1;  
                }  
                else  
                {  
                    p[i][j] = p[i][j-1];  
                }  
            }  
            else if(i!=0&&j==0)  
            {  
                if(ch1[i]==ch2[j])  
                {  
                    p[i][j] = 1;  
                }  
                else  
                {  
                    p[i][j] = p[i-1][j];  
                }  
            }  
            else  
            {  
                if(ch1[i]==ch2[j])  
                {  
                    p[i][j] = p[i-1][j-1]+1;  
                }  
                else  
                {  
                    p[i][j] = max(p[i][j-1],p[i-1][j]);  
                }  
            }  
        }  
    }  
}  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved