最长公共子串

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;
        }

   

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

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

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

 

posted on 2017-08-23 20:52  zhangxiaoyu  阅读(119)  评论(0)    收藏  举报

导航