题目
poj1080
题意:
给出两个字符串,求它们匹配成功后(两个字符串长度要相等,不相等的话可以在较短的字符串加字符 ’-‘ 增长)的最大得分。
得 分 = ∑ i = 0 s t r 1. l e n g h t ( ) − 1 ∑ j = 0 s t r 2. l e n g h t ( ) − 1 c n t ( i , j ) 得分 = \sum_{i=0}^{str1.lenght()-1} \sum_{j=0}^{str2.lenght()-1} cnt(i, j) 得分=i=0∑str1.lenght()−1j=0∑str2.lenght()−1cnt(i,j)
cnt[ ][ ]:
思路:
Needleman - Wunsch算法
dp过程:
代码
#include <iostream>
#include <algorithm>
#include <map>
#define DEBUG freopen("_in.txt", "r", stdin); freopen("_out1.txt", "w", stdout);
#define CASE int t; cin >> t; while (t--)
using namespace std;
const int MAXN = 1e2 + 10;
char a[MAXN], b[MAXN];
int dp[MAXN][MAXN];
map<char,int> m;
int cnt[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}};
void init(){
m['A'] = 0;
m['C'] = 1;
m['G'] = 2;
m['T'] = 3;
m['-'] = 4;
}
void solve(){
int lena, lenb;
scanf("%d %s", &lena, a);
scanf("%d %s", &lenb, b);
for (int i = 0; i <= lena; i++)
for (int j = 0; j <= lenb; j++)
if (i == 0 && j == 0)
dp[i][j] = 0;
else if (i == 0)
dp[i][j] = dp[i][j-1] + cnt[m['-']][m[b[j-1]]];
else if (j == 0)
dp[i][j] = dp[i-1][j] + cnt[m[a[i-1]]][m['-']];
else
dp[i][j] = max(dp[i-1][j-1]+cnt[m[a[i-1]]][m[b[j-1]]], max(dp[i][j-1]+cnt[m['-']][m[b[j-1]]], dp[i-1][j]+cnt[m[a[i-1]]][m['-']]));
printf("%d\n", dp[lena][lenb]);
}
int main(){
init();
CASE solve();
return 0;
}