2010-11-06 114 views
1

我有下面的代碼,它不因爲某些原因,我想弄清楚正常工作:尋找最大公共子

public static int score(String gene1, String gene2){ 
    char[] a=new char[gene1.length()]; 
    char[] b=new char[gene2.length()]; 
    a=gene1.toCharArray(); 
    b=gene2.toCharArray(); 
    return score(a, b, 0,0); 
} 

private static int score(char[] a, char[] b, int i, int j){ 
    if(a[i]=='\0' || b[j]=='\0') 
     return 0; 
    else if (a[i]==b[j]) 
     return 1+score(a, b, i+1, j+1); 
    else 
     return max(score(a, b,i+1, j),score(a, b, i, j+1)); 
} 

private static int max (int a, int b){ 
    if (a<b) return b; 
    else return a; 
} 

這裏是它失敗:

assertEquals(2, GeneAnalysis.score("ACGT","AC")); 

我得到一個IndexOutofBoundsError

任何想法?另外,在提供幫助時,請不要更改方法參數。他們應該是他們的樣子。這

+1

家庭作業標籤也許? – 2010-11-06 00:33:32

+0

是這功課嗎?有趣。你問了45個問題並給出了0個答案。 – smartnut007 2010-11-06 00:34:16

+0

請向我們提問這樣的問題時向我們顯示輸入,實際產量和預期產量。 – 2010-11-06 00:34:35

回答

6

部分似乎是C和Java之間的混亂....

if(a[i]=='\0' || b[j]=='\0') 
     return 0; 

C有字符串空終止,Java則沒有。相反,檢查Java數組,你將需要使用。長度屬性的結束......是這樣的:

if(i >= a.length || j >= b.length) 

編輯基於評論。

真的,你應該就這個問題提出一個單獨的問題......但這裏是一個刺探它是如何工作的。首先你使用遞歸,這是一個棘手的概念。維基百科可能可以幫助您掌握遞歸的基礎知識。

下面是代碼更接近我會怎麼寫呢,有意見顯示您的訂單發生的事情:

public class Main 
{ 
    public static void main(String[] args) 
    { 
     final int value; 

     value = score("ACGT", "AC"); 
     System.out.println(value); 
    } 

    public static int score(final String gene1, 
          final String gene2) 
    { 
     final char[] a; 
     final char[] b; 
     final int s; 

     a = gene1.toCharArray(); 
     b = gene2.toCharArray(); 
     s = score(a, b, 0, 0); 

     return (s); 
    } 

    private static int score(final char[] a, 
          final char[] b, 
          final int idxA, 
          final int idxB) 
    { 
     final int value; 

     // for all calls: a.length == 4 and b.length == 2 
     // first call: idxA == 0 and idxB == 0 - false || false == false 
     // second call: idxA == 1 and idxB == 1 - false || false == false 
     // third call: idxA == 2 and idxB == 2 - false || true == true  
     if(idxA >= a.length || idxB >= b.length) 
     { 
      // third call: will return 0    
      value = 0; 
     } 
     // first call: a[idxA] == A and b[idxB] == A - true 
     // second call: a[idxB] == C and b[idxB] == C - true 
     else if(a[idxA] == b[idxB]) 
     { 
      // this is recursion 
      // first call: 1 + score(a, b, 1, 1) 
      // second call: 1 + score(a, b, 2, 2) 

      // after the third call the call stack starts unwinding 
      // second call: 1 + 0 == 1 
      // first call: 1 + 1 == 2 
      value = 1 + score(a, b, idxA + 1, idxB + 1); 
     } 
     else 
     { 
      final int x; 
      final int y; 

      x = score(a, b, idxA + 1, idxB); 
      y = score(a, b, idxB,  idxB + 1); 
      value = max(x, y); 
     } 

     // third call: 0 
     // second call: 1 
     // first call: 2 
     return (value); 
    } 

    // Can you use Math.max(int, int) instad? 
    private static int max(final int x, final int y) 
    { 
     final int value; 

     if(x < y) 
     { 
      value = y; 
     } 
     else 
     { 
      value = x; 
     } 

     return (value); 
    } 
} 
+0

好吧,現在我有工作代碼,我意識到我非常幸運。我對它的工作原理有一點點認識,但並不全面。我試圖在紙上描繪它,但它變得非常混亂。試圖瞭解這是如何工作的好方法是什麼? – Snowman 2010-11-06 01:10:53

+0

@fprime嘗試查看我的更新答案,瞭解它如何工作的一些想法。 – TofuBeer 2010-11-06 04:51:11

3
public static int score(String gene1, String gene2){ 
    char[] a=gene1.toCharArray();//assign it directly 
    char[] b=gene2.toCharArray(); 
    return score(a, b, 0, 0); 
} 

private static int score(char[] a, char[] b, int i, int j) { 
    //check for end using length, java doesn't use that nullbyte-stuff for it 
    //this caused the failure 
    if(i==a.length || j==b.length) { 
     return 0; 
    } else if (a[i]==b[j]) { 
     return 1+score(a, b, i+1, j+1); 
    } else { 
     return max(score(a, b, i+1, j), score(a, b, i, j+1)); 
    } 
} 

private static int max (int a, int b){ 
    if (a<b) return b; 
    return a; 
} 
+2

通常最好解釋發生了什麼問題,而不是僅僅發佈固定代碼。 – 2010-11-06 00:46:40

+0

@Mark E:已編輯。這種方式還是隻是一個解釋? – thejh 2010-11-06 00:50:33

+0

看起來不錯,很好的編輯。 – 2010-11-06 00:51:19

0
char[] a=new char[gene1.length()]; 
a=gene1.toCharArray(); 

嗯。在第一行中分配的數組會發生什麼?