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

1110 - An Easy LCS

PDF (English) Statistics Forum
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;
}
时间: 2024-08-24 13:16:23

lightoj 1110 - An Easy LCS 最长公共子序列+string的相关文章

LightOJ 1110 An Easy LCS LCS路径输出

点击打开链接题目链接 1110 - An Easy LCS PDF (English) Statistics Forum 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.

lcs(最长公共子序列),dp

lcs(最长公共子序列) 求两个序列的lcs的长度,子序列可不连续 dp[i][j]=dp[i-1][j-1]+1(a[i]==b[i]) dp[i][j]=max(dp[i-1][j],dp[i][j-1])(a[i]!=b[i]) memset(dp,0,sizeof(dp)); for(int i=1;i<=n1;i++){ for(int j=1;j<=n2;j++){ if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(

LCS 最长公共子序列(DP经典问题)

最长公共子序列问题以及背包问题都是DP(动态规划)算法的经典题目,值得深度挖掘以致了解DP算法思想.问题如下: 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最

算法设计 - LCS 最长公共子序列&amp;&amp;最长公共子串 &amp;&amp;LIS 最长递增子序列

出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的最长公共子串方法.最长公共子串用动态规划可实现O(n^2)的时间复杂度,O(n^2)的空间复杂度:还可以进一步优化,用后缀数组的方法优化成线性时间O(nlogn):空间也可以用其他方法优化成线性.3.LIS(最长递增序列)DP方法可实现O(n^2)的时间复杂度,进一步优化最佳可达到O(nlogn)

POJ 2250(LCS最长公共子序列)

compromise Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is n

LCS(最长公共子序列)和dp(动态规划)

参照:v_JULY_v 最长公共子序列定义: 注意最长公共子串(Longest CommonSubstring)和最长公共子序列(LongestCommon Subsequence, LCS)的区别:子串(Substring)是串的一个连续的部分,子序列(Subsequence)则是从不改变序列的顺序,而从序列中去掉任意的元素而获得的新序列:更简略地说,前者(子串)的字符的位置必须连续,后者(子序列LCS)则不必.比如字符串acdfg同akdfc的最长公共子串为df,而他们的最长公共子序列是ad

LIS(最长递增子序列)和LCS(最长公共子序列)的总结

最长公共子序列(LCS):O(n^2) 两个for循环让两个字符串按位的匹配:i in range(1, len1) j in range(1, len2) s1[i - 1] == s2[j - 1], dp[i][j] = dp[i - 1][j -1] + 1; s1[i - 1] != s2[j - 1], dp[i][j] = max (dp[i - 1][j], dp[i][j - 1]); 初始化:dp[i][0] = dp[0][j] = 0; 伪代码: dp[maxn1][ma

POJ - 1458 Common Subsequence (LCS最长公共子序列)

题意: 给出两个字符串,求出最长的公共子序列大小. 思路: 算是最经典的LCS问题吧. 设 \(X=(x_1,x_2,.....x_n) 和 Y=(y_1,y_2,.....y_m)\) 是两个序列,将 X 和 Y 的最长公共子序列记为\(lcs(X,Y)\) ,找出\(lcs(X,Y)\)就是一个最优问题 然后我们需要将其分解成子问题并求出子问题的最优解: (寻找子问题来推出当前问题,正是寻找状态转移方程最重要的一步) 1)如果 \(x_n=y_m\),即X的最后一个元素与Y的最后一个元素相同

Coincidence(LCS) 最长公共子序列问题

题目描述: Find a longest common subsequence of two strings. 输入: First and second line of each input case contain two strings of lowercase character a…z. There are no spaces before, inside or after the strings. Lengths of strings do not exceed 100. 输出: Fo