最长公共子串(LCS)(连续与非连续)

连续子串         

         1.思路

               使用count[i][j]来表示s1[0......i-1]和s2[0......j-1]的最长公共子串。(即,c[1][1]表示s1的第一个字符构成的字符串与s2的第一个字符,特殊的,c[0][x]表示不包含s1字符的字符串和s2部分字符串公共字串,显然是0)。

               因为子串必须是连续的,所以对于s1[i]和y[j]而言,他们要么和之前的公共子串构成新的公共子串,要么就不能构成公共子串。所以,如果不能构成公共子串,count[i][j]为0,否则,为之前公共子串长度+1。所以,状态转移方程为:

                1) s1[i] == s2[j]     count[i][j] = count[i-1][j-1] +1

                2)s1[i] != s2[j]      count[i][j] = 0

                 初始化时,对于i=0或j=0,表示不含字符,count[i][j] = 0


         2.代码

<pre name="code" class="java">		Scanner cin = new Scanner(System.in);
		String s1 = cin.next();
		String s2 = cin.next();
<span style="white-space:pre">		</span>//因为0表示的是没有字符,所以必须到length+1才能包含字符串中所有字符		
		int[][] count = new int[s1.length()+1][s2.length()+1];
		int max = 0;
		int begin=0,end=0;//用来记录最长公共子串的开始和结束位置
		
		for(int i=0; i<=s1.length(); i++)
		{
			for(int j=0; j<=s2.length(); j++)
			{<pre name="code" class="java"><span style="white-space:pre">				</span>//因为0为不包含任何字符,所以所有下标为0的count均为0
if(i==0 || j==0)count[i][j] = 0;}}//因为从1才表示是第一个字符for(int i=1; i<=s1.length(); i++){for(int j=1; j<=s2.length(); j++){
<span style="white-space:pre">				</span>//第i个字符在s1中下标为i-1
if(s1.charAt(i-1) == s2.charAt(j-1)){count[i][j] = count[i-1][j-1]+1;if(max<count[i][j]){max = count[i][j];begin = i-max;end = i-1;}}elsecount[i][j] = 0;}}System.out.println(max);System.out.println(s1.substring(begin, end+1));
 


 


                                            非连续子串

                  1.思路

                     方法同上,状态转移方程为

                   1) s1[i] == s2[j]     count[i][j] = count[i-1][j-1] +1

                   2)s1[i] != s2[j]      count[i][j] = max{count[i-1][j],count[i][j-1]}

                 初始化时,对于i=0或j=0,表示不含字符,count[i][j] = 0

  2.代码:

		int[] a = {0, 1, 2, 3, 5};
		int[] b = {1, 3, 5, 7, 9};
		
		int[][] count = new int[a.length+1][b.length+1];
		int max = -1;
		
		for(int i=0; i<a.length; i++)
		{
			for(int j=0; j<b.length; j++)
			{
				if(i==0 || j==0)
					count[i][j] = 0;
			}
		}
		for(int i=1; i<=a.length; i++)
		{
			for(int j=1; j<=b.length; j++)
			{
				if(a[i-1]==b[j-1])
					count[i][j] = count[i-1][j-1]+1;
				else
					count[i][j] = Math.max(count[i][j-1], count[i-1][j]);
				
				if(max<count[i][j])
					max = count[i][j];
			}
		}
		System.out.println(max);






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值