0

我将创建一个 needleman-wunsch 全局序列比对。但我的回答是错误的。请帮我检查我的代码。有时当两个序列匹配时,它仍然会运行不匹配函数。忽略我蹩脚的英语,谢谢。

#include <iostream>
using namespace std;

int max(int a , int b , int c ) ; // declaration

int main (){

  string A = "ABBC" ;
  string B = "CBAC" ;
  int a = 3 ; // match
  int b = -1 ; // mismatch
  int c = -2 ; // gap
  int i , j ;

    // calculate the string size
    int m = A.size();
    int n = B.size();

    //construct a table
    int M[m+1][n+1] = {0} ;

    //insert first row and column with gap
    for (i=0; i<=m ;i++){
        M[i][0]=i*c;
    }
    for (j=0; j<=n ;j++){
        M[0][j]=j*c;
    }

  //calculate the whole matrix
    for (j=1; j<=n ; j++){
        for (i=1 ; i<=m ; i++){
            if (A[i-1] == B[j-1]){
                M[i][j] = M[i-1][j-1] + a ;
            }else{
                M[i][j] = max(M[i-1][j-1]+b ,
                              M[i-1][j]+c ,
                              M[i][j-1]+c);
            }
        }
    }
        //PRINT
    cout << "        ";
    for(  i = 0; i <= m; i++ ){
        cout << A[i] << "   ";
    }
    cout << "\n  ";

    for(  i = 0; i <= n; i++ ){
        if( i > 0 ){
            cout << B[i-1] << " ";
       }

        for(  j = 0; j <= m; j++ ){
            cout.width( 3 );
            cout << M[i][j] << " ";
        }
        cout << endl;
    }
}

// function for maximum value
int max(int a , int b , int c ){
    if(a>b && a>c){
        return a ;
    }else if(b>a && b>c){
        return b ;
    }else {
        return c ;
    }
}

这是我的输出(突出显示的是错误答案):

在此处输入图像描述

4

0 回答 0