String(hdu4681,最长公共序列+枚举)

本文详细介绍了如何通过计算最长公共序列并结合特定子串的条件,找出满足要求的最长字符串D及其长度。通过分别记录子串在主字符串中的完整出现位置,正反方向求最长公共序列,然后枚举这些位置来计算符合条件的序列长度。

http://acm.hdu.edu.cn/showproblem.php?pid=4681

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 939    Accepted Submission(s): 344

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.

 

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.

 

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: 3

Hint

For test one, D is "aaaa", and for test two, D is "acf".

 

 

Source

2013 Multi-University Training Contest 8

 

Recommend

zhuyuanchen520

 

Statistic | Submit | Discuss | Note

解析:

题意:

给出a,b,c三组字符串,

求出a,b最长公共序列长度且这个序列必须包含有c作为它的子串

思路:

最长公共序列+枚举 ;

既然所求序列必须含有子串c,那么在计算时就必须考虑这一个因素

如何处理呢?

1.分别记录ca,b,中完整出现的起止位置

2.正反方向求一遍a,b的最长公共序列长度

3.枚举a,b起止位置,求起止位置左右区间的最公共序列长度

再把结果加上c的长度即可

468MS 8896K 1766 B C++

*/

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include <iostream>
using namespace std;
const int maxn=1000+50;
char s1[maxn],s2[maxn],s3[maxn];
int dp1[maxn][maxn],dp2[maxn][maxn];
int d1[maxn][3],d2[maxn][3];
int t1,t2;
int max(int a,int b)
{
	return a>b? a:b;
}
void init()
{
	memset(dp1,0,sizeof(dp1));
	memset(dp2,0,sizeof(dp2));
	memset(d1,-1,sizeof(d1));
	memset(d2,-1,sizeof(d2));
}
void getst(char a[],char b[],int f)
{
	int n,ns,i,j,t;
	n=strlen(a);
	ns=strlen(b);
	for(i=0;i<n;i++)
	 {
	 	if(a[i]!=b[0])
	 	continue;
	 		for(j=0,t=i;t<n;t++)
	 		{
	 		if(a[t]==b[j])
	 		  {
			   j++;
			   if(j==ns)
	 		   break;
			  }
			}
		    if(j==ns)
		   {
		  	if(f==0)
		  	{
			d1[t1][0]=i;
			d1[t1++][1]=t;
		  	}
		  	else
		  	{d2[t2][0]=i;
		  	d2[t2++][1]=t;
		  	}
		   }
	 }

}
int main()
{
	int T,i,j,n,ca=0;
	int ans,n1,n2,n3;
	scanf("%d",&T);
	while(T--)
	{   init();
		scanf("%s",s1);
		scanf("%s",s2);
		scanf("%s",s3);
		printf("Case #%d: ",++ca);
		n1=strlen(s1);
		n2=strlen(s2);
		n3=strlen(s3);
		for(i=1;i<=n1;i++)//正序
		for(j=1;j<=n2;j++)
		{
			if(s1[i-1]==s2[j-1])
			dp1[i][j]=dp1[i-1][j-1]+1;
			else
			dp1[i][j]=max(dp1[i-1][j],dp1[i][j-1]);

		}

		for(i=n1-1;i>=0;i--)//逆序
		{
			for(j=n2-1;j>=0;j--)
			{
				if(s1[i]==s2[j])
				dp2[i][j]=dp2[i+1][j+1]+1;
				else
				dp2[i][j]=max(dp2[i][j+1],dp2[i+1][j]);

			}

	    }

		t1=t2=ans=0;
		getst(s1,s3,0);
		getst(s2,s3,1);
		for(i=0;i<t1;i++)
		for(j=0;j<t2;j++)
		{
			ans=max(ans,dp1[d1[i][0]][d2[j][0]]+dp2[d1[i][1]+1][d2[j][1]+1]);
		}
		ans=ans+n3;
		printf("%d\n",ans);
	}
	return 0;
}


### HDU 1159 最长公共序列 (LCS) 解题思路 #### 动态规划状态定义 对于两个字符串 `X` 和 `Y`,长度分别为 `n` 和 `m`。设 `dp[i][j]` 表示 `X[0...i-1]` 和 `Y[0...j-1]` 的最长公共序列的长度。 当比较到第 `i` 个字符和第 `j` 个字符时: - 如果 `X[i-1]==Y[j-1]`,那么这两个字符可以加入之前的 LCS 中,则有 `dp[i][j]=dp[i-1][j-1]+1`[^3]。 - 否则,如果 `X[i-1]!=Y[j-1]`,那么需要考虑两种情况中的最大值:即舍弃 `X[i-1]` 或者舍弃 `Y[j-1]`,因此取两者较大者作为新的 LCS 长度,即 `dp[i][j]=max(dp[i-1][j], dp[i][j-1])`。 时间复杂度为 O(n*m),其中 n 是第一个字符串的长度而 m 是第二个字符串的长度。 #### 实现代码 以下是 Python 版本的具体实现方式: ```python def lcs_length(X, Y): # 初始化二维数组用于存储中间结果 m = len(X) n = len(Y) # 创建(m+1)x(n+1)大小的表格来保存子问题的结果 dp = [[0]*(n+1) for _ in range(m+1)] # 填充表项 for i in range(1, m+1): for j in range(1, n+1): if X[i-1] == Y[j-1]: dp[i][j] = dp[i-1][j-1] + 1 else: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) return dp[m][n] # 测试数据输入部分可以根据具体题目调整 if __name__ == "__main__": while True: try: a = input().strip() b = input().strip() result = lcs_length(a,b) print(result) except EOFError: break ``` 此程序会读入多组测试案例直到遇到文件结束符(EOF)。每组案例由两行组成,分别代表要计算其 LCS 的两个字符串。最后输出的是它们之间最长公共序列的长度。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值