Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 158 Accepted Submission(s): 69
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=1100; char a[N],b[N],c[N]; int dp1[N][N],dp2[N][N],loc[N<<2][2],cnt; //dp1[][]表示字符串a,b从前往后的最大公共子串,dp2[][]表示字符串a,b从后往前的最大公共子串 //loc[][]记录字符串c在a,b中出现的第一个位置和最后一个位置 int len1,len2,len3; void solve(char *str,int len){ //求出loc[][] int i,j,k; for(i=1;i<=len;i++){ if(str[i]==c[1]){ for(j=i,k=1;j<=len && k<=len3;j++) if(str[j]==c[k]) k++; if(k!=len3+1) break; loc[cnt][0]=i; loc[cnt][1]=j-1; cnt++; } } } int main(){ //freopen("input.txt","r",stdin); int t,cases=0; scanf("%d",&t); while(t--){ scanf("%s%s%s",a+1,b+1,c+1); len1=strlen(a+1); len2=strlen(b+1); len3=strlen(c+1); memset(dp1,0,sizeof(dp1)); for(int i=1;i<=len1;i++) for(int j=1;j<=len2;j++) if(a[i]==b[j]) dp1[i][j]=dp1[i-1][j-1]+1; else dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1]); memset(dp2,0,sizeof(dp2)); for(int i=len1;i>=1;i--) for(int j=len2;j>=1;j--) if(a[i]==b[j]) dp2[i][j]=dp2[i+1][j+1]+1; else dp2[i][j]=max(dp2[i+1][j],dp2[i][j+1]); cnt=0; solve(a,len1); int x=cnt; solve(b,len2); int y=cnt-x,ans=0; for(int i=0;i<x;i++) for(int j=0;j<y;j++) ans=max(ans,dp1[loc[i][0]-1][loc[j+x][0]-1]+dp2[loc[i][1]+1][loc[j+x][1]+1]); printf("Case #%d: %d\n",++cases,ans+len3); } return 0; }