**1.27C基因相似補好思路(難)

思路:通過數組統計不同的基因組合所對應的值,然後定義兩個不計長度的vector數組用於儲存不同的基因型。(所對應的類型組合對應表格。)然後定義一個函數進行遞歸調用,用於判斷可能出現的可能型組合,輸出最大值即爲所需要的基因型形似度。然後註釋解釋代碼作用。

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <string.h>
using namespace std;
int loss_weight[5][5]={{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,-100000}};
int scores[1000][1000]={0};
int dp(int x,int y,vector<int>gener_a,vector<int>gener_b,int len_a,int len_b){
    if(scores[x][y]!=0)
        return scores[x][y];
    if(x==len_a){
        for(int it = y ;it<len_b;++it){
            scores[x][y]+=loss_weight[4][gener_b[it]];
        }
        return scores[x][y];
    }
    if(y==len_b){
        for(int it = x ;it<len_a;++it){
            scores[x][y]+=loss_weight[gener_a[it]][4];
        }
        return scores[x][y];
    }
    int choice_a = dp(x+1,y+1,gener_a,gener_b,len_a,len_b)+loss_weight[gener_a[x]][gener_b[y]];//計算的是從某個數開始往後所一一對應的組合,計算和。例如(a[0],b[0];a[1],b[1];a[2],b[2]等等)
    int choice_b = dp(x,y+1,gener_a,gener_b,len_a,len_b)+loss_weight[4][gener_b[y]];//利用y+1進行移位後用遞歸重複上面可以一一計算出組合的值的相似度之和。這裏作用只是進行移位,因爲其職肯定沒有算出一一對應的數組大。
    int choice_c = dp(x+1,y,gener_a,gener_b,len_a,len_b)+loss_weight[gener_a[x]][4];//同上面作用。

這裏肯定要把x或y的數組掃描一遍纔會打出最大值。
    scores[x][y]=max(choice_a,max(choice_b,choice_c));
    return scores[x][y];
}
int main()
{

    int test_case;
    cin>>test_case;
    while(test_case--){
        int a_len,b_len;
        string a,b;
        cin>>a_len>>a;
        vector<int>gener_a;
        vector<int>gener_b;
        cin>>b_len>>b;
        memset(scores,0,sizeof(scores));
        for(auto it = a.begin();it!=a.end();++it){
            if(*it=='A')
                gener_a.push_back(0);
            else if(*it=='C')
                gener_a.push_back(1);
            else if(*it=='G')
                gener_a.push_back(2);
            else if(*it=='T')
                gener_a.push_back(3);
            else
                gener_a.push_back(4);
        }
         for(auto it = b.begin();it!=b.end();++it){
            if(*it=='A')
                gener_b.push_back(0);
            else if(*it=='C')
                gener_b.push_back(1);
            else if(*it=='G')
                gener_b.push_back(2);
            else if(*it=='T')
                gener_b.push_back(3);
            else
                gener_b.push_back(4);
        }
        cout<<dp(0,0,gener_a,gener_b,a_len,b_len)<<endl;
    }
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章