LCS最长子串问题



方法一:动态规划 效率O(mn)(mn是分别是两个字符串的长度)

#include<iostream>
using namespace std;

int c[100][100];//全局变量自动初始化为0
inline int max(int a, int b)
{
	return (a > b ? a : b);
}

int LCS(const char *X,const char *Y)
{
	if (NULL == X || NULL == Y)
		return 0;
	int xlen = strlen(X);
	int ylen = strlen(Y);
	for (int i = 1; i <= xlen; i++)
	{
		for (int j = 1; j <= ylen; j++)
		{
			if (X[i-1] == Y[j-1])
				c[i][j] = c[i - 1][j - 1] + 1;
			else
				c[i][j] = max(c[i][j - 1], c[i - 1][j]);
		}
	}
	return c[xlen][ylen];
}
int main()
{
	char A[8] = "ABCBDAB";
	char B[8] = "BDCABAB";
	cout << LCS(A, B) << endl;
	system("pause");
	return 0;
}


方法二:低效的递归算法

#define INF 9999999
int c[100][100];
int LCS_Memo(const char* X, const char* Y,int i,int j)
{
	if (c[i][j] < INF)
		return c[i][j];
	if (0 == i || 0 == j)
		c[i][j] = 0;
	else if (X[i - 1] == Y[j - 1])
		c[i][j] = LCS_Memo(X,Y,i - 1, j - 1) + 1;
	else
	{
		int p = LCS_Memo(X, Y, i - 1, j);
		int q = LCS_Memo(X, Y, i, j - 1);
		if (p >q)
			c[i][j] = p;
		else
		{
			c[i][j] = q;
		}
	}
	return c[i][j];
}
int LCS(const char* X ,const char* Y)
{
	if (NULL == X || NULL == Y)
		return 0;
	int xlen = strlen(X);
	int ylen = strlen(Y);
	memset(c, INF, sizeof(c));//注意是将每个字节赋值为INF,不是一个int
	
	return LCS_Memo(X, Y, xlen, ylen);
}
int main()
{
	char A[8] = "ABCBDAB";
	char B[8] = "BDCABAB";
	cout << LCS(A, B) << endl;
	system("pause");
	return 0;
}

用java实现LCS:

public class LCS {
	public static int[][] c=new int[100][100];//这样创建每个int默认为0,
											  //如果只是声明int[][] c;c的默认值为null
	public static int max(int a,int b)
	{
		return (a>b?a:b);
	}
	
	public static int LCSValue(String a,String b)//String类作为参数
	{
		if(null == a || null == b)
		{
			return 0;
		}
		
		int xlen=a.length();
		int ylen=b.length();
		for(int i=1;i<=xlen;i++)
		{
			for(int j=1;j<=ylen;j++)
			{
				if(a.charAt(i-1)==b.charAt(j-1))//String类下标访问
					c[i][j]=c[i-1][j-1]+1;
				else
					c[i][j]=max(c[i][j-1],c[i-1][j]);
			}
		}
		
		return c[xlen][ylen];
		
	}
	
	public   int ChLCSValue(char a[],char b[])//char数组作为参数
	{
		if(null == a || null == b)
		{
			return 0;
		}
		
		int xlen=a.length;//String类就是a.length();
		int ylen=b.length;
		for(int i=1;i<=xlen;i++)
		{
			for(int j=1;j<=ylen;j++)
			{
				if(a[i-1]==b[j-1])
					c[i][j]=c[i-1][j-1]+1;
				else
					c[i][j]=max(c[i][j-1],c[i-1][j]);
			}
		}
		
		return c[xlen][ylen];
		
	}
	
	public static void main(String args[])//静态方法不能使用非静态变量
	{
		String A="ABCBDAB";		
		String B="BDCABA";
		int res = LCSValue(A,B);//静态方法只能调用静态方法,所以LCSValue声明为static
		
		/*
		 * 数组的初始化有三种:char cha[] = {'a','2','3','f'}; (2)char[]  cha=  new char[]{'a','2','3','f'};(3)
		 * char[]  cha=  new char[4];cha[0]='a';cha[1]='2';...
		 */
		char a[]={'A','B','D'};	//java中的char数组初始化	
		char b[]={'B','A','B','D'};	
		
		LCS lcs = new LCS();
		int resCh = lcs.ChLCSValue(a,b);//可以利用引用对象来调用非静态方法ChLCSValue
		
		System.out.print(res+"\n"+resCh);
	}
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值