题意:给定2个基因串(ACGT),可以往里面添加’-‘(可表示任意字母),使2串长度相同,两串添加完的最大相似度。。
思路:dp
f[i][j]表示第一串前i个,第二串前j个添加完的最大相似du
f[i][j] = max(f[i -1][j-1] + mat(s1[i] , s2[j]), f[i-1][j] +mat('-', s2[j]), f[i][j-1] + mat(s1[i],'-'));
mat(char1, char2) 表示两字符的相似度。。
1 /* 2 State:Accepted 3 Time:2013.3.1 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cstring> 8 #include <string> 9 #include <cstdlib> 10 #include <algorithm> 11 #include <cmath> 12 #include <algorithm> 13 #define CLR(NAME) memset(NAME , 0 ,sizeof(NAME)); 14 using namespace std; 15 const int w[5][5] = 16 {{5 , -1 , -2 , -1 , -3}, 17 {-1 , 5 , -3 , -2 , -4}, 18 {-2 , -3 , 5 , -2 , -2}, 19 {-1 , -2 , -2 , 5 , -1}, 20 {-3 , -4 , -2 , -1 , 0}}; 21 22 int test , len1, len2 ,f[200][200]; 23 char s1[200] ,s2[200]; 24 25 26 void init(){ 27 scanf("%d",&len1); 28 scanf("%s",s1 + 1); 29 scanf("%d",&len2); 30 scanf("%s",s2 + 1); 31 } 32 33 int find(char word){ 34 if (word == 'A') return 0; 35 if (word == 'C') return 1; 36 if (word == 'G') return 2; 37 if (word == 'T') return 3; 38 if (word == '-') return 4; 39 40 } 41 int mat(char word1 ,char word2){ 42 int x = find(word1); 43 int y = find(word2); 44 return w[x][y]; 45 46 } 47 void dp(){ 48 for (int i = 0; i <= len1; ++i) 49 for (int j = 0; j <= len2; ++j){ 50 if (i == 0 && j == 0) f[i][j] = 0; 51 if (i == 0 && j != 0) f[0][j] = f[0][j - 1] + mat('-' , s2[j]); 52 if (i != 0 && j == 0) f[i][0] = f[i - 1][0] + mat(s1[i] , '-'); 53 } 54 55 for (int i = 1; i <= len1; ++i) 56 for (int j = 1; j <= len2; ++j){ 57 f[i][j] = f[i - 1][j - 1] + mat(s1[i] ,s2[j]); 58 f[i][j] = max(f[i][j] , f[i - 1][j] + mat(s1[i] , '-')); 59 f[i][j] = max(f[i][j] , f[i][j - 1] + mat('-' , s2[j])); 60 } 61 printf("%d\n",f[len1][len2]); 62 } 63 64 int main(){ 65 freopen("poj1080.in","r",stdin); 66 freopen("poj1080.out","w",stdout); 67 scanf("%d",&test); 68 while ( test-- ){ 69 init(); 70 dp(); 71 } 72 fclose(stdin); 73 fclose(stdout); 74 }