POJ 1080 Human Gene Functions

http://poj.org/problem?id=1080

题意:

给出两段由ACGT组成的字符串,可以在字符串中插入-,将字符串进行匹配,上图为匹配值,输出最大的匹配值。

 

思路:

对于每一组匹配,它都有3种选择,第一是不插入-直接匹配,第二种是第一个字符串插入-再匹配,第三种是第二个字符串插入-再匹配,具体看代码。

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 100 + 5;
 9 
10 int n1, n2;
11 char s1[maxn], s2[maxn];
12 int score[maxn][maxn];
13 int d[maxn][maxn];
14 
15 void init()
16 {
17     score['A']['A'] = 5;
18     score['C']['C'] = 5;
19     score['G']['G'] = 5;
20     score['T']['T'] = 5;
21     score['-']['-'] = -5;
22     score['A']['C'] = score['C']['A'] = -1;
23     score['A']['G'] = score['G']['A'] = -2;
24     score['A']['T'] = score['T']['A'] = -1;
25     score['A']['-'] = score['-']['A'] = -3;
26     score['C']['G'] = score['G']['C'] = -3;
27     score['C']['T'] = score['T']['C'] = -2;
28     score['C']['-'] = score['-']['C'] = -4;
29     score['G']['T'] = score['T']['G'] = -2;
30     score['G']['-'] = score['-']['G'] = -2;
31     score['T']['-'] = score['-']['T'] = -1;
32 }
33 
34 int main()
35 {
36     //freopen("D:\\txt.txt", "r", stdin);
37     int T;
38     scanf("%d", &T);
39     init();
40     while (T--)
41     {
42         scanf("%d%s", &n1, s1 + 1);
43         scanf("%d%s", &n2, s2 + 1);
44 
45         d[0][0] = 0;
46         for (int i = 1; i <= n1; i++)
47             d[i][0] = d[i - 1][0] + score[s1[i]]['-'];
48         for (int i = 1; i <= n2; i++)
49             d[0][i] = d[0][i - 1] + score['-'][s2[i]];
50         for (int i = 1; i <= n1; i++)
51         {
52             for (int j = 1; j <= n2; j++)
53             {
54                 d[i][j] = max(d[i-1][j] + score[s1[i]]['-'], max(d[i][j - 1] + score['-'][s2[j]],d[i-1][j-1]+score[s1[i]][s2[j]]));
55             }
56         }
57         cout << d[n1][n2] << endl;
58     }
59 }

 

posted @ 2017-03-17 20:58  Kayden_Cheung  阅读(155)  评论(0编辑  收藏  举报
//目录