String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 152 Accepted Submission(s): 66
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
本题要求满足上述条件的最长公共子序列,是个比较经典的DP问题。
本题的难点在于求得的最长公共子序列要满足包含D(D是它的子串),可以知道C是D在两头加入若干字符,中间不能加。我们可以分别在A,B中暴力枚举最近的包含子串(去掉若干字符后包含子串D)D的某段的起始s、终点位置e。注意暴力枚举最近的。最终答案有三部分组成:
LCS(A[0-s-1],B[0-s-1])+len(D)+LCS(A[e+1,len(A)],B[e+1,len(B)])
比赛的时候没有想到与处理开头和末尾的最长公共子串,也没有想到最近优化,导致一直超时。
模仿大牛的代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN=1000+10;
char str1[MAXN],str2[MAXN],str3[MAXN];
vector<pair<int,int> >p1,p2;
int dp[MAXN][MAXN];//保存正向的最长公共子序列
int rev_dp[MAXN][MAXN];//保存逆向的最长公共子序列
void cal_str(char s1[],char s2[],vector<pair<int,int> >&p)
{
int i,j,k;
int len1=strlen(s1+1);
int len2=strlen(s2+1);
p.clear();
for(i=1;i<=len1;i++)
{
k=1;
if(s1[i]!=s2[k])
{
continue;
}
for(j=i;j<=len1;j++)
{
if(s1[j]==s2[k])
{
k++;
}
if(k>len2)
{
break;
}
}
if(k>len2)
{
p.push_back(make_pair(i,j));
}
}
}
int main()
{
int i,j,cas,len1,len2,len3,ans,tag=1;
cin>>cas;
while (cas--)
{
scanf("%s%s%s",str1+1,str2+1,str3+1);
len1=strlen(str1+1);
len2=strlen(str2+1);
len3=strlen(str3+1);
cal_str(str1,str3,p1);
cal_str(str2,str3,p2);
ans = 0;
memset(dp,0,sizeof(dp));
memset(rev_dp,0,sizeof(rev_dp));
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(str1[i]==str2[j])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
{
dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
}
}
}
for(i=len1;i>0;i--)
{
for(j=len2;j>0;j--)
{
if(str1[i]==str2[j])
{
rev_dp[i][j]=rev_dp[i+1][j+1]+1;
}
else
{
rev_dp[i][j]=max(rev_dp[i+1][j], rev_dp[i][j+1]);
}
}
}
for(i=0;i<(int)p1.size();i++)
{
for(j=0;j<(int)p2.size();j++)
{
ans=max(ans,len3+dp[p1[i].first-1][p2[j].first-1]+rev_dp[p1[i].second+1][p2[j].second+1]);
}
}
printf("Case #%d: %d\n",tag++,ans);
}
return 0;
}
自己的超时的代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int MAXN=1000+10;
int dp[MAXN][MAXN];
int *ra1,cnt_ra1;
int *ra2,cnt_ra2;
int mem[2][MAXN];
int ll;
char str1[MAXN],str2[MAXN],str3[MAXN],tstr1[MAXN],tstr2[MAXN];
int rlen,llen;
int max(int a,int b)
{
return a<b?b:a;
}
void max_length(char *s1,char *s2,int *ra,int &cnt_ra)
{
int len1=strlen(s1);
int len2=strlen(s2);
int i,j;
memset(dp,0,sizeof(dp));
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(s1[i-1]==s2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
int tmp=dp[len1][len2];
rlen=llen=tmp;
cnt_ra=0;
for(i=1;i<=len1;i++)
{
if(dp[i][len2]==tmp)
ra[cnt_ra++]=i;
}
}
int LCI(char *s1,char *s2)
{
int len1=strlen(s1);
int len2=strlen(s2);
int i,j;
memset(dp,0,sizeof(dp));
for(i=1;i<=len1;i++)
{
for(j=1;j<=len2;j++)
{
if(s1[i-1]==s2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
int tmp=dp[len1][len2];
ll=0;
for(i=1;i<=len1;i++)
{
if(tmp==dp[i][len2])
{
ll=i;
return tmp;
}
}
}
int main()
{
int cas,i,j,k,mid,ans,ansl,ansr,l1,l2,tag=1;
char ch;
cin>>cas;
while(cas--)
{
ans=0;
ra1=mem[0];ra2=mem[1];
scanf("%s",str1);
scanf("%s",str2);
scanf("%s",str3);
max_length(str1,str3,ra1,cnt_ra1);
max_length(str2,str3,ra2,cnt_ra2);
for(i=0;i<cnt_ra1;i++)
{
for(j=0;j<cnt_ra2;j++)
{
strcpy(tstr1,str1);strcpy(tstr2,str2);
ansr=LCI(tstr1+ra1[i],tstr2+ra2[j]);
mid=(ra1[i]+0)/2;
for(k=0;k<mid;k++)
{
ch=tstr1[k];
tstr1[k]=tstr1[ra1[i]-k-1];
tstr1[ra1[i]-k-1]=ch;
}
tstr1[ra1[i]]=0;
mid=(ra2[j]+0)/2;
for(k=0;k<mid;k++)
{
ch=tstr2[k];
tstr2[k]=tstr2[ra2[j]-k-1];
tstr2[ra2[j]-k-1]=ch;
}
tstr2[ra2[j]]=0;
int l=strlen(str3);
mid=(l+0)/2;
for(k=0;k<mid;k++)
{
ch=str3[k];
str3[k]=str3[l-k-1];
str3[l-k-1]=ch;
}
str3[l]=0;
LCI(tstr1,str3);
l1=ll;
LCI(tstr2,str3);
l2=ll;
ansl=LCI(tstr1+l1,tstr2+l2);
if(ans<ansr+ansl)
ans=ansr+ansl;
}
}
printf("Case #%d: %d\n",tag++,strlen(str3)+ans);
}
return 0;
}