POJ 1080 Human Gene Functions
题意:
给出两组 \(DNA\) 序列求它们的最大相似度
每个字母与其他字母或自身和空格对应都有一个打分,求在这两个字符串中插入空格,让这两个字符串的匹配分数最大?
思路:
类似最短编辑距离
定义 \(f[i][j]\) 为第一组序列的 \([1,i]\) 与第二组序列的 \([1,j]\) 匹配对应的最大相似度。
转移方程: \(f[i][j] = max{f[i - 1][j - 1] + ma[a[i]][b[j]],f[i][j - 1] + ma['-'][b[j]],f[i - 1][j] + ma[a[i]]['-']]}\)
注意这里的 \(f[i][0]\) 和 \(f[0][i]\) 都需要初始化
实现:
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 150;
char ma[N][N], a[N], b[N];
int f[N][N];
void init()
{
ma['A']['A'] = 5;
ma['A']['C'] = ma['C']['A'] = -1;
ma['A']['G'] = ma['G']['A'] = -2;
ma['A']['T'] = ma['T']['A'] = -1;
ma['A']['-'] = ma['-']['A'] = -3;
ma['C']['C'] = 5;
ma['C']['G'] = ma['G']['C'] = -3;
ma['C']['T'] = ma['T']['C'] = -2;
ma['C']['-'] = ma['-']['C'] = -4;
ma['G']['G'] = 5;
ma['G']['T'] = ma['T']['G'] = -2;
ma['G']['-'] = ma['-']['G'] = -2;
ma['T']['T'] = 5;
ma['T']['-'] = ma['-']['T'] = -1;
}
int main()
{
init();
int _;
scanf("%d", &_);
while (_--)
{
int n, m;
scanf("%d%s", &n, a + 1);
scanf("%d%s", &m, b + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
f[i][j] = 0;
for (int i = 1; i <= n; i++)
f[i][0] = f[i - 1][0] + ma[a[i]]['-'];
for (int i = 1; i <= m; i++)
f[0][i] = f[0][i - 1] + ma['-'][b[i]];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
{
f[i][j] = f[i - 1][j - 1] + ma[a[i]][b[j]];
f[i][j] = max(f[i][j], f[i][j - 1] + ma['-'][b[j]]);
f[i][j] = max(f[i][j], f[i - 1][j] + ma[a[i]]['-']);
}
printf("%d\n", f[n][m]);
}
return 0;
}
标签:Functions,ma,1080,int,POJ,Gene
From: https://www.cnblogs.com/zxr000/p/17002027.html