Skip to content

Commit 4047e5f

Browse files
committed
20190604
1 parent a690cad commit 4047e5f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

ZJU1027.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <cstdio>
2+
#include <iostream>
3+
#include <cstdlib>
4+
using namespace std;
5+
const int MaxN = 101;
6+
7+
// 两个基因序列
8+
char str1[MaxN], str2[MaxN];
9+
// 记录动态规划过程中产生的中间结果
10+
// gene[i][j]表示基因子串str1[0...i-1]和str2[0...j-1]的分值
11+
int gene[MaxN][MaxN];
12+
// 基因分值矩阵
13+
int score[5][5] = {{5, -1, -2, -1, -3},
14+
{-1, 5, -3, -2, -4},
15+
{-2, -3, 5, -2, -2},
16+
{-1, -2, -2, 5, -1},
17+
{-3, -4, -2, -1, 0}};
18+
19+
int get_max(int num1, int num2, int num3)
20+
{
21+
int result = num1;
22+
if (num2 > result)
23+
result = num2;
24+
if (num3 > result)
25+
result = num3;
26+
return result;
27+
}
28+
29+
int convert(char c)
30+
{
31+
switch (c)
32+
{
33+
case 'A':
34+
return 0;
35+
case 'C':
36+
return 1;
37+
case 'G':
38+
return 2;
39+
case 'T':
40+
return 3;
41+
case '-':
42+
return 4;
43+
}
44+
return 5;
45+
}
46+
47+
int main()
48+
{
49+
int n;
50+
int len_a, len_b;
51+
cin >> n;
52+
for (int k = 0; k < n; k++)
53+
{
54+
cin >> len_a >> str1;
55+
cin >> len_b >> str2;
56+
57+
gene[0][0] = 0;
58+
for (int i = 1; i <= len_b; i++)
59+
gene[0][i] = gene[0][i - 1] + score[4][convert(str2[i - 1])];
60+
for (int i = 1; i <= len_a; i++)
61+
gene[i][0] = gene[i - 1][0] + score[convert(str1[i - 1])][4];
62+
int m1, m2, m3;
63+
for (int i = 1; i <= len_a; i++)
64+
for (int j = 1; j <= len_b; j++)
65+
{
66+
// 三种情况
67+
m1 = gene[i - 1][j] + score[convert(str1[i - 1])][4];
68+
m2 = gene[i][j - 1] + score[4][convert(str2[j - 1])];
69+
m3 = gene[i - 1][j - 1] + score[convert(str1[i - 1])][convert(str2[j - 1])];
70+
gene[i][j] = get_max(m1, m2, m3);
71+
}
72+
cout << gene[len_a][len_b] << endl;
73+
}
74+
system("pause");
75+
return 0;
76+
}

0 commit comments

Comments
 (0)