/*
translation:
题目大意是给出两个基因序列,要你求出这两个基因序列的相似度。所谓的相似度,
就是将基因串用‘-’填充使得两串长度相同后,将对应位置上的字母一一对应,再
根据题目给出的表计算出分数,由于用‘-’填充的做法有好几种,所以得分最高的那个
就作为相似度。
solution:
基本dp,形如LCS的转移方程。
d[i][j]:串1选到第i-1个,串2选到第j-1个,所得到的最高分数。d[len1][len2]
既是所求答案。
转移方程:d(i,j)=max(d(i-1,j-1)+s(i,j), d[i-1][j] + s[a][4], d[i][
j-1] + s[4][b])
note:
之所以wa是因为边界的初始化不正确,原本是按照LCS的写法。但其实是错误的,具体见
代码注释
date:
2016.8.20
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 105;
char g0[maxn], g1[maxn];
int s[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 d[maxn][maxn], n0, n1;
int chToNum(char c) {
if(c == 'A') return 0;
if(c == 'C') return 1;
if(c == 'G') return 2;
if(c == 'T') return 3;
}
int main()
{
//freopen("input.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%s", &n0, g0);
scanf("%d%s", &n1, g1);
//printf("g0:%s\ng1:%s\n", g0, g1);
memset(d, 0, sizeof(d));
for(int i = 1; i <= n0; i++) { //note!!!
int t = chToNum(g0[i-1]);
d[i][0] = d[i-1][0] + s[t][4];
}
for(int j = 1; j <= n1; j++) { //因为根据状态的含义,在0位置前面可选的只有‘-’
int t = chToNum(g1[j-1]);
d[0][j] = d[0][j-1] + s[4][t];
}
for(int i = 1; i <= n0; i++) {
for(int j = 1; j <= n1; j++) {
int a = chToNum(g0[i-1]), b = chToNum(g1[j-1]);
int t = d[i-1][j-1] + s[a][b];
d[i][j] = max(t, max(d[i-1][j] + s[a][4], d[i][j-1] + s[4][b]));
}
}
printf("%d\n", d[n0][n1]);
}
return 0;
}
poj1080(LCS类的转移方程)
最新推荐文章于 2020-11-05 15:55:47 发布