lightoj 1110 - An Easy LCS 最长公共子序列+string

1110 - An Easy LCS
Time Limit: 2 second(s)Memory Limit: 32 MB

LCS means 'Longest Common Subsequence' that means two non-empty strings are given; you have to find the Longest Common Subsequence between them. Since there can be many solutions, you have to print the one which is the lexicographically smallest. Lexicographical order means dictionary order. For example, 'abc' comes before 'abd' but 'aaz' comes before 'abc'.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. The next two lines will contain two strings of length 1 to 100. The strings contain lowercase English characters only.

Output

For each case, print the case number and the lexicographically smallest LCS. If the LCS length is 0 then just print ':('.

Sample Input

Output for Sample Input

3

 

ab

ba

 

zxcvbn

hjgasbznxbzmx

 

you

kjhs

Case 1: a

Case 2: zxb

Case 3: :(

 




题意:找出最长公共自序列,有一样长的取字典序小的。如果lcs长度为0,输出哭脸。

做法:在最长公共子序列基础上,每次dp的时候 判断下两个string的字典序大小。最长公共子序列的复杂度是100*100,字符串最长是100,O(10^6)。


#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
using namespace std;
#define MAX_N 110//再大dp数组会爆的   O(n*m)
char a[MAX_N],b[MAX_N];
int dp[MAX_N+1][MAX_N+1];
string dps[MAX_N][MAX_N];
int mxxx(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n,m,i,j,tem;
	int t;
	int cas=1;
	scanf("%d",&t);
	while(t--)//a b链的长度
	{
		memset(dp,0,sizeof(dp));//dp中ij 代表a链前i项  和b链 前j项  最大公共子序列长度
		scanf("%s",a);
		scanf("%s",b);
		n=strlen(a);
		m=strlen(b);
		
		for(i=0;i<=n;i++)
			for(int j=0;j<=m;j++)
				dps[i][j].clear();
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				if(a[i]==b[j])//dp的ij 从1开始算
				{ 
					
					if(dp[i+1][j]>dp[i][j+1])
						dps[i+1][j+1]=dps[i+1][j];
					else if(dp[i+1][j]<dp[i][j+1])
						dps[i+1][j+1]=dps[i][j+1];
					else if(dps[i+1][j]<dps[i][j+1])
						dps[i+1][j+1]=dps[i+1][j];
					else
						dps[i+1][j+1]=dps[i][j+1];
					dp[i+1][j+1]=mxxx(dp[i+1][j],dp[i][j+1]);

					if(dp[i+1][j+1]>dp[i][j]+1)
						dps[i+1][j+1]=dps[i+1][j+1];
					else if(dp[i+1][j+1]<dp[i][j]+1)
						dps[i+1][j+1]=dps[i][j]+a[i];
					else if(dps[i+1][j+1]>dps[i][j]+a[i])
						dps[i+1][j+1]=dps[i][j]+a[i];
					else
						dps[i+1][j+1]=dps[i+1][j+1]; 

					dp[i+1][j+1]=mxxx(dp[i][j]+1,mxxx(dp[i+1][j],dp[i][j+1]));
				}
				else
				{
					if(dp[i+1][j]>dp[i][j+1])
						dps[i+1][j+1]=dps[i+1][j];
					else if(dp[i+1][j]<dp[i][j+1])
						dps[i+1][j+1]=dps[i][j+1];
					else if(dps[i+1][j]<dps[i][j+1])
						dps[i+1][j+1]=dps[i+1][j];
					else
						dps[i+1][j+1]=dps[i][j+1];
					dp[i+1][j+1]=mxxx(dp[i+1][j],dp[i][j+1]);
				}
			}
		}
		printf("Case %d: ",cas++);
		if(dp[n][m])
		cout<<dps[n][m]<<endl;
		else
			puts(":(");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值