两个字符串的最长公共子序列的长度

两个字符串的最长公共子序列(max common sequence)

九度OJ链接:http://ac.jobdu.com/problem.php?pid=1042

两个字符串的最长公共子序列是指给定两个字符串,找出这两个字符串中相同的字符的序列,这些字符不要求连续(注意与最长公共子串的区别,最长公共子串要求字符是连续的

举个例子,比如字符串 X: "FABCDF" , Y: "ABFDE"

X和Y的最长公共子序列长度为 3,即 "ABD",注意"AB"和"D"并没有连续。

X和Y的最长公共子串的长度为2,即"AB"


假设 L(Xi,Yj) 表示X字符串从0-i的子串与Y字符串从0-j的子串的最长公共子序列长度,CX[i]表示字符串X在索引为i处的字符,CY[j]表示字符串Y在索引为j处的字符,则有(状态转移方程):

L(Xi,Yj)=L(X(i-1),Y(j-1))+1,  如果CX[i]==CY[j];

L(Xi,Yj)=max( L(X(i-1),Yj) , L(Xi,Y(j-1)) ),  如果CX[i]!=CY[j]

就上面的例子来看,由于CX[5]='F', CY[4]='E',即CX[5]!=CY[4],因此L(X5,Y4)=max( L(X4,Y4) , L(X5,Y3) ),即字符串"FABCDF"与"ABFD"的最大公共子序列与字符串"FABCD"与"ABFDE"的最大公共子序列二者的较大者


递归代码:

	public int maxCommomSubSequence(String s1,String s2){
		if(s1.length()<=0 || s2.length()<=0)
			return 0;
		if(s1.length()==1 &&s2.length()==1){
			return s1.charAt(0)==s2.charAt(0)?1:0;
		}
		if(s1.charAt(s1.length()-1)==s2.charAt(s2.length()-1)){
			return 1+maxCommomSubSequence(s1.substring(0, s1.length()-1),s2.substring(0,s2.length()-1));
		}else
			return Math.max(maxCommomSubSequence(s1.substring(0, s1.length()-1),s2.substring(0,s2.length())),
					maxCommomSubSequence(s1.substring(0, s1.length()),s2.substring(0,s2.length()-1)));			
	}


非递归代码:

	public int maxCommomSubSequence2(String s1,String s2){
		//用于保存Xi Yj 两个字符串的最大公共子序列长度
		int[][] commonSubSqsLen=new int[s1.length()][s2.length()];
		int maxLen=0;//表示两个字符串的最大公共子序列长度
		for(int i=0;i<s1.length();i++){
			for(int j=0;j<s2.length();j++){
				if(s1.charAt(i)==s2.charAt(j)){
					if(i-1>=0&&j-1>=0){
						commonSubSqsLen[i][j]=1+commonSubSqsLen[i-1][j-1];
					}else{
						commonSubSqsLen[i][j]=1;
					}
				}else{
					if(i-1>=0){
						if(j-1>=0){
							commonSubSqsLen[i][j]=Math.max(commonSubSqsLen[i][j-1],commonSubSqsLen[i-1][j]);
						}else{
							commonSubSqsLen[i][j]=commonSubSqsLen[i-1][j];
						}
					}else{
						if(j-1>=0){
							commonSubSqsLen[i][j]=commonSubSqsLen[i][j-1];
						}else{
							commonSubSqsLen[i][j]=0;
						}
					}				
				}
				if(commonSubSqsLen[i][j]>maxLen)
					maxLen=commonSubSqsLen[i][j];
			}
		}
		for(int i=0;i<commonSubSqsLen.length;i++) //将矩阵打印出来
			System.out.println(Arrays.toString(commonSubSqsLen[i]));
		return maxLen;
	}
以上面这个例子,输出结果为:

[0, 0, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 2, 2, 2, 2]
[1, 2, 2, 2, 2]
[1, 2, 2, 3, 3]
[1, 2, 3, 3, 3]
3



延伸扩展:

1. 求出两个字符串的所有最大公共子序列?

2. 求两个字符串的最大公共子串?




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zlp1992

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值