最长公共子序列算法实现

改代码准备实现最长公共子序列,但是不知道哪里错了,代码如下:

public class LCS {
    public static int LCS(char[] P,char[] Q,int m,int n){
        m=P.length;
        n=Q.length;
        int max = 0;
        if(m==-1||n==-1)
        {
            return 0;
        }
        if(P[m-1]==Q[n-1])
        {
             max = LCS(P,Q,m-1,n-1)+1;
        }
        if(P[m-1]!=Q[n-1])
        {
            int a = LCS(P,Q,n-1,m);
            int b = LCS(P,Q,n,m-1);
            max = a>b?a:b;

        }
        return max;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        char[] array1 = {'t','g','c','a','t','g','a'};
        char[] array2 = {'a','t','c','g','g','a'};
        int max = LCS(array1,array2,array1.length,array2.length);
        System.out.print(max);
    }

}
 

 

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^