您的位置:首页 > 其它

poj 1080 Human Gene Functions

2012-08-20 09:05 183 查看
/*
假设输入的字符串分别为 A,B
f[i][j] = { 从 A[i] 和 B[j] 开始匹配,所能达到的最大值 }
假设 A[i] = G,B[j] = C
那么现在的情况就是
Gxxxxx
Cxxxxx
状态转移为
=> f[i + 1][j] + value(A[i], '-')
G...
-C..

=> f[i][j + 1] + value(B[j], '-')
-G..
C...

=> f[i + 1][j + 1] + value(A[i], B[j])
G...
C...
*/
//最近的LCS题都要看题解才可以写成代码,DP还不够熟悉,要多加练习才可以!come on!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <memory.h>
using namespace std;
const int MAX = 110;
int str1[MAX], str2[MAX], ans[MAX][MAX];

int value[][5] = {{0, -3, -4, -2, -1},
{-3, 5, -1, -2, -1},
{-4, -1, 5, -3, -2},
{-2, -2, -3, 5, -2},
{-1, -1, -2, -2, 5}};

int change(char ch)
{
switch(ch){
case 'A': return 1;
case 'C': return 2;
case 'G': return 3;
case 'T': return 4;
}
}

int main()
{
int i, j, tc, len1, len2, tmp1, tmp2, tmp;
char ch;
cin >> tc;
while (tc--){
cin >> len1;
for (i = 1; i <= len1; i++){
cin >> ch;
str1[i] = change(ch);
}
cin >> len2;
for (i = 1; i <= len2; i++){
cin >> ch;
str2[i] = change(ch);
}

memset(ans, 0, sizeof(ans));
for (i = 1; i <= len1; i++){
ans[i][0] = value[str1[i]][0] + ans[i-1][0];
}
for (i = 1; i <= len2; i++){
ans[0][i] = value[0][str2[i]] + ans[0][i-1];
}

for (i = 1; i <= len1; i++){
for (j = 1; j <= len2; j++){
tmp = value[str1[i]][str2[j]] + ans[i-1][j-1];
tmp1 = ans[i-1][j] + value[str1[i]][0];
tmp2 = ans[i][j-1] + value[0][str2[j]];
tmp1 = max(tmp1, tmp2);
ans[i][j] = max(tmp, tmp1);
}
}

cout << ans[len1][len2] << endl;
}

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