hdu 4681 String (DP,4级)
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 122 Accepted Submission(s): 48
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.
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.
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: 3HintFor test one, D is "aaaa", and for test two, D is "acf".
Source
Recommend
zhuyuanchen520
思路:AB公共+子串+AB公共
其实挺水的,居然没出,最后一小时想出来了,敲完代码还剩15分钟提交,出了个BUG,差了17秒再次提交果断悲剧了。
调的不忍直视
#include<iostream> #include<cstring> #include<map> #include<cstdio> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) #define ll(x) (1<<x) using namespace std; const int mm=1009; char A[mm],B[mm],D[mm]; int hh[mm][mm],tt[mm][mm]; int pos,sss,ttt; class Edge { public:int l,r; }AA[mm],BB[mm]; int pa,pb; int look(char*s,int x,int len,char*t) { int j=x;int kn=strlen(t); for(int i=0;t[i];++i,++j) { while(t[i]!=s[j]&&j<=len)++j; if(t[i]!=s[j])return 0; if(j>=len+1&&i<kn)return 0; } return j-1; } void debug(int f[][mm],int a,int b) { for(int i=0;i<=a;++i) for(int j=0;j<=b;++j) { printf("%d%c",f[i][j],j==b?'\n':' '); } } int main() { int cas; while(~scanf("%d",&cas)) { FOR(ca,1,cas) { printf("Case #%d: ",ca); scanf("%s%s%s",A+1,B+1,D); int la,lb,ld; la=strlen(A+1); lb=strlen(B+1); ld=strlen(D); clr(hh,0);clr(tt,0); FOR(i,1,la)FOR(j,1,lb) { hh[i][j]=max(hh[i-1][j],hh[i][j-1]); if(A[i]==B[j])hh[i][j]=max(hh[i][j],hh[i-1][j-1]+1); } //debug(hh,la,lb); // puts("+++++++++++++"); for(int i=la;i>=0;--i) for(int j=lb;j>=0;--j) { tt[i][j]=max(tt[i+1][j],tt[i][j+1]); if(A[i]==B[j]) tt[i][j]=max(tt[i][j],tt[i+1][j+1]+1); } // debug(tt,la,lb); //printf("ce= %d %d\n",hh[la][lb],tt[0][0]); pa=pb=0; for(int i=1;A[i];++i) if(A[i]==D[0]) { int z=look(A,i,la,D); if(z==0)continue; //puts("++"); // printf("A=%d %d\n",i,z); AA[pa].l=i;AA[pa].r=z;pa++; } for(int i=1;B[i];++i) if(B[i]==D[0]) { int z=look(B,i,lb,D); if(z==0)continue; BB[pb].l=i;BB[pb].r=z;pb++; } int ans=0,ll,rr; FOR(i,0,pa-1)FOR(j,0,pb-1) { //ll=min(AA[i].l,BB[j].l); //rr=max(AA[i].r,BB[i].r); ans=max(ans,hh[ AA[i].l-1 ][ BB[j].l-1 ]+tt[ AA[i].r+1 ][ BB[j].r+1 ] ); } //printf("ans=%d\n",ans); ans+=ld; printf("%d\n",ans); } } }
The article write by nealgavin