POJ 1080 Human Gene Functions(DP)
这个匹配矩阵,很有特点,鼓励相同,鼓励匹配,不鼓励匹配空。状态转移很类似于LCS,初始化做好,OK了。
1 #include <cstring> 2 #include <string> 3 #include <cstdio> 4 #include <iostream> 5 using namespace std; 6 int c[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}}; 7 char s1[101],s2[101]; 8 int dp[101][101]; 9 int judge(char a) 10 { 11 if(a == 'A') 12 return 0; 13 else if(a == 'C') 14 return 1; 15 else if(a == 'G') 16 return 2; 17 else if(a == 'T') 18 return 3; 19 return 0; 20 } 21 int main() 22 { 23 int t,len1,len2,i,j,x,y,sum; 24 scanf("%d",&t); 25 while(t--) 26 { 27 memset(dp,0,sizeof(dp)); 28 scanf("%d%s%d%s",&len1,s1,&len2,s2); 29 sum = 0; 30 for(i = 1;i <= len1;i ++) 31 { 32 x = judge(s1[i-1]); 33 y = judge(s2[0]); 34 dp[i][1] = sum + c[x][y]; 35 dp[i][1] = max(dp[i-1][1]+c[x][4],dp[i][1]); 36 sum += c[x][4]; 37 } 38 sum = 0; 39 for(i = 1;i <= len2;i ++) 40 { 41 x = judge(s1[0]); 42 y = judge(s2[i-1]); 43 dp[1][i] = sum + c[x][y]; 44 dp[1][i] = max(dp[1][i-1]+c[4][y],dp[1][i]); 45 sum += c[4][y]; 46 } 47 for(i = 2; i <= len1;i ++) 48 { 49 for(j = 2;j <= len2;j ++) 50 { 51 x = judge(s1[i-1]); 52 y = judge(s2[j-1]); 53 dp[i][j] = dp[i-1][j-1] + c[x][y]; 54 dp[i][j] = max(dp[i-1][j]+c[x][4],dp[i][j]); 55 dp[i][j] = max(dp[i][j-1]+c[4][y],dp[i][j]); 56 } 57 } 58 printf("%d\n",dp[len1][len2]); 59 } 60 return 0; 61 }