2010-11-06 116 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(); 

嗯。在第一行中分配的数组会发生什么?