最长公共子序列(可打印LCS)

动态规划算法

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;

#define MAXSTRLEN 20

int Lcs(char x[], char y[], int path[][MAXSTRLEN])//求序列x和y的最长公共子序列,path保存路径指向,以方便打印公共子序列
{
	int i, j;
	int len1=strlen(x)-1;
	int len2=strlen(y)-1;
	
	int **c=new int*[len1+1];
	for(i=0; i<=len1; i++)
		c[i]=new int[len2+1];
	
	for(i=0; i<=len1; i++)
		c[i][0]=0;
	for(i=0; i<=len2; i++)
		c[0][i]=0;
	for(i=1; i<=len1; i++)
		for(j=1; j<=len2; j++)//从x[1],y[1]开始
		{
			if(x[i]==y[j])
			{
				c[i][j]=c[i-1][j-1]+1;
				path[i][j]=1;
			}
			else if(c[i-1][j]>=c[i][j-1])
			{
				c[i][j]=c[i-1][j];
				path[i][j]=2;
			}
			else
			{
				c[i][j]=c[i][j-1];	
				path[i][j]=3;
			}		
		}
		
		return c[len1][len2];
}

void PrintLcs(int i, int j, char x[], int path[][MAXSTRLEN])//打印最长公共子序列
{
	if(i==0 || j==0)
		return;
	
	if(path[i][j]==1)
	{
		PrintLcs(i-1, j-1, x, path);
		cout<<x[i];
	}
	else if(path[i][j]==2)
		PrintLcs(i-1, j, x, path);
	else
		PrintLcs(i, j-1, x, path);
	
	
	
}
void main()
{
	char a[MAXSTRLEN];
	char b[MAXSTRLEN];
	int path[MAXSTRLEN][MAXSTRLEN];
	gets(a+1);//a[0]不算,从a[1]开始
	gets(b+1);//b[0]不算,从b[1]开始
	
	cout<<Lcs(a, b, path)<<endl;
	cout<<"最长公共子序列:";
	PrintLcs(strlen(a)-1, strlen(b)-1, a, path);
	cout<<endl;
	
}


递归算法

#include <iostream>
using namespace std;

#define MAXSTRLEN 20

//递归算法
int Lcs(char *str1, char *str2)
{
	if(*str1=='\0' || *str2=='\0')
		return 0;
	if(*str1==*str2)
		return Lcs(str1+1, str2+1)+1;
	else if(Lcs(str1+1, str2)>Lcs(str1, str2+1))
		return Lcs(str1+1, str2);
	else
		return Lcs(str1, str2+1);
	
}

void main()
{
	char a[MAXSTRLEN];
	char b[MAXSTRLEN];
	
	gets(a);
	gets(b);
	cout<<Lcs(a, b)<<endl;
	
}


 

### C语言实现最长公共子序列(LCS)算法 #### 动态规划求解LCS问题 为了高效地解决问题,可以采用动态规划方法。该方法通过构建二维数组`c[i][j]`来存储两个字符串前缀的最大匹配长度,并借助辅助矩阵`b[i][j]`追踪状态转移路径。 ```c #include <stdio.h> #include <string.h> void LCSLength(char *X, char *Y, int m, int n, int c[][100], int b[][100]) { for (int i = 0; i <= m; ++i) c[i][0] = 0; for (int j = 0; j <= n; ++j) c[0][j] = 0; for (int i = 1; i <= m; ++i) for (int j = 1; j <= n; ++j){ if (X[i-1] == Y[j-1]){ c[i][j] = c[i-1][j-1] + 1; b[i][j] = 1; // 表示来自左上角 } else if(c[i-1][j] >= c[i][j-1]){ c[i][j] = c[i-1][j]; b[i][j] = 2; // 来自上方 }else{ c[i][j] = c[i][j-1]; b[i][j] = 3; // 来自左边 } } } // 打印LCS函数 void PrintLCS(int b[][100],char *X,int i,int j){ if(i==0 || j==0)return ; if(b[i][j]==1){PrintLCS(b,X,i-1,j-1);printf("%c",X[i-1]);} else if(b[i][j]==2)PrintLCS(b,X,i-1,j); else PrintLCS(b,X,i,j-1); } int main(){ char X[]="ABCBDAB"; char Y[]="BDCABA"; int m=strlen(X),n=strlen(Y); int c[m+1][100],b[m+1][100]; LCSLength(X,Y,m,n,c,b); printf("The longest common subsequence is:"); PrintLCS(b,X,strlen(X),strlen(Y)); printf("\nThe length of the longest common subsequence is:%d\n",c[m][n]); return 0; } ``` 此程序实现了基于动态规划的最长公共子序列计算功能[^1]。它不仅返回了最大长度还提供了具体的子序列内容。对于输入字符串"ABCBDAB"和"BDCABA"而言,这段代码会输出它们之间的任意一条最长公共子序列及其对应的长度7中的4。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值