您的位置:首页 > 其它

HDU 1080 Human Gene Functions

2013-04-10 22:13 337 查看
最长公共子序列的变形题,算法导论公共子序列后的练习题有题是类似的

注意在公共子序列中要判断a[i]和b[j]相等,而此题是考虑相似,不要求相等

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>

#define MAX 100 + 5
using namespace std;

//dp[i][j]表示字符串a以a[i]结尾,字符串b以b[j]结尾的最大相似度
int dp[MAX][MAX];

int score[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,0}
};

int main(){
map<char,int> table;
table['A'] = 0;table['C'] = 1;table['G'] = 2;
table['T'] = 3;table['-'] = 4;
int T;
cin >> T;
while(T--){
int na,nb;
string stra,strb;
cin >> na>>stra >> nb>>strb;
memset(dp,0,sizeof(dp));
//注意边界条件
for(int i = 1; i <= na; i ++ ) dp[i][0] = dp[i-1][0] + score[ table[ stra[i-1] ] ][ table[ '-' ] ];
for(int i = 1; i <= nb; i ++ ) dp[0][i] = dp[0][i-1] + score[ table['-'] ][ table[ strb[i-1] ] ];

for(int i = 1 ; i <= na; i ++ ){
for(int  j = 1; j <= nb; j ++){
dp[i][j]= max(dp[i- 1  ][j -1 ] + score[ table[ stra[i-1] ] ][ table[ strb[j-1] ]],
max(dp[i- 1  ][j] + score[ table[ stra[i-1] ]][ table[ '-' ]],  dp[i][j-1] + score[ table[ '-' ]][ table[ strb[j-1] ]]) );
}
}
cout<<dp[na][nb]<<endl;

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: