码迷,mamicode.com
首页 > 其他好文 > 详细

最长公共子串

时间:2017-08-23 21:45:13      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:最长公共子串   公共子串   static   情况   for   stat   array   log   ring   

https://my.oschina.net/gaosheng/blog/308853

public static int LCS(char query[], char text[]) { int len_query = query.length; int len_text= text.length; //数组c记录匹配情况,模拟二维矩阵 int[] c = new int[len_text]; int len, i, j; len=0; for(i=0; i<len_query; i++) { //不反过来会把之前数组元素冲掉的,因为后面的元素需要根据前面的元素计算 for(j=len_text-1; j>=0; j--) { if(query[i] == text[j]) { if(i==0 || j==0) c[j]=1; else c[j]=c[j-1]+1; } else c[j] = 0; if(c[j] > len) len=c[j]; } System.out.println(Arrays.toString(c)); } return len; }

  

 

LCS(new char[]{‘a‘,‘b‘,‘c‘,‘d‘},new char[]{‘b‘,‘c‘,‘d‘});

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

 

最长公共子串

标签:最长公共子串   公共子串   static   情况   for   stat   array   log   ring   

原文地址:http://www.cnblogs.com/joshsung/p/7420135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!