当前位置: 代码迷 >> Eclipse >> 初学者求解一个算法有关问题
  详细解决方案

初学者求解一个算法有关问题

热度:62   发布时间:2016-04-23 13:35:31.0
菜鸟求解一个算法问题
动态规划求最长子串的问题,初学,从算法书上改的...求找错并解答
[code=Java][/code]package suanfa2;

import java.lang.reflect.Array;

public class LCString {
static int[][] c;
public static int tempi=0,tempj=0;
int lcsLength(char[] x,char[] y){
int m = Array.getLength(x);
int n = Array.getLength(y);
c = new int[m+1][n+1];
for (int i = 1;i <= m;i++){
c[i][0] = 0;
}
for (int j = 0;j <= n;j++){
c[0][j] = 0;
}
for(int i = 1;i <= m; i++){
for(int j = 1;j <= n;j++){
if (x[i-1]==y[j-1]){
c[i][j] = c[i - 1][j - 1] + 1;
}

else{
c[i][j] = 0;
}
}
}
int max = c[0][0];
for(int i = 0;i <= m;i++){
for(int j = 0;j <= n;j++){
if(c[i][j] > max){
max = c[i][j];
tempi = i;
tempj = j;
}
}
}
System.out.println("tempi="+tempi+" tempj="+tempj+" c[tempi][tempj]="+c[tempi][tempj]);
return c[tempi][tempj];
}

void printLCS(char[] x,char[] y,int i,int j){
if(i==0 || j==0)
return;
if(x[i-1] == y[j-1]){
printLCS(x,y,i-1,j-1);
System.out.print(x[i-1]);
}
else return;

}

public static void main(String[] args){
LCString lcs = new LCString();
String X1="yzyzzyx";
String Y1="zxyyzxz";
char[] x1 = X1.toCharArray();
char[] y1 = Y1.toCharArray();
lcs.lcsLength(x1,y1);
lcs.printLCS(x1,y1,LCString.tempi,LCString.tempi);
System.out.println();

String X2 = "MAEEEVAKLEKHLMLLRQEYVKLQKKLAETEKRCALLAAQANKESSSESFISRLLAIVAD";  
String Y2 = "MAEEEVnKLEKHLMLLRQEYVKLQKKLAETEKRCTLLAAQANKENSNESFISRLLAIVAG";
char[] x2 = X2.toCharArray();
char[] y2 = Y2.toCharArray();
lcs.lcsLength(x2, y2);
lcs.printLCS(x2,y2, LCString.tempi, LCString.tempj);
}




}
[code=Java][/code]


运行结果是:tempi=2 tempj=7 c[tempi][tempj]=2
tempi=34 tempj=34 c[tempi][tempj]=27
KLEKHLMLLRQEYVKLQKKLAETEKRC

第一个子串不能输出,应该是 printLCS函数有错,但我调了半天没调出来,求大家解答,不胜感激!

------解决方案--------------------
看下我的吧
Java code
public class Test {    public static void main(String[] args){        int[] a = {2,1,2};        int[] b = {3,1,2,1};        largestCommonSub(a,b);}    public static void largestCommonSub(int[] a,int[] b){        int[][] arr = new int[a.length][b.length];        int maxLength=0;        for(int i=0;i<a.length;i++){            for(int j=0;j<b.length;j++){                if(a[i]==b[j])                    if(i==0||j==0)                        arr[i][j] = 1;                    else                        arr[i][j] = arr[i-1][j-1]+1;                if(arr[i][j]>maxLength)                    maxLength = arr[i][j];            }        }//        for(int i=0;i<a.length;i++)//            {for(int j=0;j<b.length;j++)//            {//                System.out.print(arr[i][j]+" ");//            }//            System.out.println();//            }        for(int i=0;i<a.length;i++)            for(int j=0;j<b.length;j++)        {            if(arr[i][j]==maxLength){                int start = j-maxLength+1;                for(int x=0;x<maxLength;x++)                    System.out.print(b[start+x]+" ");            System.out.println();                }        }    } }
  相关解决方案