标签:最长公共子串 公共子串 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