/*
http://poj.org/problem?id=1080 Human Gene Functions
最长公共子序列 (变体)
dp[i][j]: x串为i长度 y串为j长度 的最大匹配值
*/
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define CLR(c,v) (memset(c,v,sizeof(c)))
using namespace std;
template <typename _T>
inline _T Max(_T a , _T b){
return (a > b) ? (a) : (b);
}
template <typename _T>
inline _T Max(_T a , _T b,_T c){
return (a > Max(b,c)) ? (a) : Max(c,b);
}
int map[5][5];
const int A = 0;
const int C = 1;
const int G = 2;
const int T = 3;
const int _ = 4;
int GetSub(char a){
switch(a){
case 'A':return 0;
case 'C':return 1;
case 'G':return 2;
case 'T':return 3;
case '_':return 4;
default: return -1;
}
}
int dp[105][105]; // dp[i][j]: x串为i长度 y串为j长度 的最大匹配值
int main(){
map[A][A] = 5; map[A][C] = -1; map[A][G] = -2; map[A][T] = -1; map[A][_] = -3;
map[C][A] = -1; map[C][C] = 5; map[C][G] = -3; map[C][T] = -2; map[C][_] = -4;
map[G][A] = -2; map[G][C] = -3; map[G][G] = 5; map[G][T] = -2; map[G][_] = -2;
map[T][A] = -1; map[T][C] = -2; map[T][G] = -2; map[T][T] = 5; map[T][_] = -1;
map[_][A] = -3; map[_][C] = -4; map[_][G] = -2; map[_][T] = -1; map[_][_] = -0xffffff;
freopen("in.txt","r",stdin);
int Ncase ;cin >> Ncase;
while(Ncase--){
int len1 , len2;
string a,b;
cin >> len1 >> a >> len2 >> b;
CLR(dp,0);
dp[0][0] = 0;
for(int i = 1 ; i <= len1 ; i++){
dp[i][0] = dp[i-1][0] + map[ GetSub(a[i-1]) ][ _ ];
}
for(int j = 1 ; j <= len2 ; j++){
dp[0][j] = dp[0][j-1] + map[ _ ][ GetSub(b[j-1]) ];
}
for(int i = 1 ;i <= len1 ; i++){
for(int j = 1 ; j <= len2 ; j++){
int aa = map[ GetSub(a[i-1]) ][ GetSub(b[j-1]) ];
int bb = map[ _ ][ GetSub(b[j-1]) ];
int cc = map[ GetSub(a[i-1]) ][ _ ];
dp[i][j] = Max(dp[i-1][j-1] + aa , dp[i][j-1] + bb , dp[i-1][j] + cc);
}
}
cout << dp[len1][len2] << endl;
}
return 0;
}
POJ 1080 Human Gene Functions -- dp最长公共子序列 (变体)
最新推荐文章于 2024-03-08 21:21:32 发布