10723 - Cyborg Genes

#include <stdio.h>
#include <string.h>
#define LL long long
#define maxn 35
LL d[maxn][maxn][maxn],fd[maxn][maxn],ls1,ls2;
char s1[maxn],s2[maxn];
LL max(LL a,LL b)
{
	if(a>b) return a;
	else return b;
}
LL dp(int l1,int l2,int l3)
{
	LL &res=d[l1][l2][l3];
	if(res!=-1) return res;
	if(l1>l3||l2>l3) return res=0;
	if(l1==0&&l2==l3||l2==0&&l1==l3) return res=1;
	if(l1==0&&l2!=l3||l2==0&&l1!=l3) return res=0;
	if(s1[l1]==s2[l2]) res=dp(l1-1,l2-1,l3-1);
	else res=dp(l1-1,l2,l3-1)+dp(l1,l2-1,l3-1);
	return res;
}
int main()
{
	int t;
	scanf("%d",&t);
	getchar();
	for(int tt=1;tt<=t;tt++)
	{
		gets(s1+1);
		gets(s2+1);
		ls1=strlen(s1+1);
		ls2=strlen(s2+1);
		memset(fd,0,sizeof(fd));
		for(int i=1;i<=ls1;i++)
		{
			for(int j=1;j<=ls2;j++)
			{
				if(s1[i]==s2[j])
				fd[i][j]=fd[i-1][j-1]+1;
				else 
				fd[i][j]=max(max(fd[i-1][j-1],fd[i-1][j]),fd[i][j-1]);
			}
		}
		memset(d,-1,sizeof(d));
		printf("Case #%d: %lld %lld\n",tt,ls1+ls2-fd[ls1][ls2],dp(ls1,ls2,ls1+ls2-fd[ls1][ls2]));
	}
	return 0;
}

你可能感兴趣的:(10723 - Cyborg Genes)