Skip to content

Commit ba6efe9

Browse files
committed
20190606
1 parent ed86f38 commit ba6efe9

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

ZJU1027.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1+
// @Author 贾立威 201710405130
12
#include <cstdio>
23
#include <iostream>
34
#include <cstdlib>
45
using namespace std;
56
const int MaxN = 101;
67

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];
128
// 基因分值矩阵
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}};
9+
const int score[5][5] = {{5, -1, -2, -1, -3},
10+
{-1, 5, -3, -2, -4},
11+
{-2, -3, 5, -2, -2},
12+
{-1, -2, -2, 5, -1},
13+
{-3, -4, -2, -1, 0}};
1814

15+
// 求三个数最大值
1916
int get_max(int num1, int num2, int num3)
2017
{
2118
int result = num1;
@@ -26,6 +23,7 @@ int get_max(int num1, int num2, int num3)
2623
return result;
2724
}
2825

26+
// 将AGCT-转化为整数进行处理
2927
int convert(char c)
3028
{
3129
switch (c)
@@ -46,27 +44,41 @@ int convert(char c)
4644

4745
int main()
4846
{
47+
// 每组测试用例数量
4948
int n;
49+
// 两个基因序列
50+
char str1[MaxN], str2[MaxN];
51+
// 第一个序列的长度,第二个序列的长度
5052
int len_a, len_b;
53+
// 记录动态规划过程中产生的中间结果
54+
// gene[i][j]表示基因子串str1[0...i-1]和str2[0...j-1]的分值
55+
int gene[MaxN][MaxN];
56+
5157
cin >> n;
5258
for (int k = 0; k < n; k++)
5359
{
5460
cin >> len_a >> str1;
5561
cin >> len_b >> str2;
5662

5763
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])];
6064
for (int i = 1; i <= len_a; i++)
65+
// 第一条序列碱基仅匹配‘-’时
6166
gene[i][0] = gene[i - 1][0] + score[convert(str1[i - 1])][4];
67+
for (int i = 1; i <= len_b; i++)
68+
// 第二条序列碱基仅匹配‘-’时
69+
gene[0][i] = gene[0][i - 1] + score[4][convert(str2[i - 1])];
70+
6271
int m1, m2, m3;
6372
for (int i = 1; i <= len_a; i++)
6473
for (int j = 1; j <= len_b; j++)
6574
{
66-
// 三种情况
67-
m1 = gene[i - 1][j] + score[convert(str1[i - 1])][4];
75+
// 第一条序列碱基仅匹配‘-’时
76+
m1 = gene[i - 1][j] + score[convert(str1[i - 1])][4];
77+
// 第二条序列碱基仅匹配‘-’时
6878
m2 = gene[i][j - 1] + score[4][convert(str2[j - 1])];
79+
// 两条序列碱基相互匹配时
6980
m3 = gene[i - 1][j - 1] + score[convert(str1[i - 1])][convert(str2[j - 1])];
81+
// 选取最优情况
7082
gene[i][j] = get_max(m1, m2, m3);
7183
}
7284
cout << gene[len_a][len_b] << endl;

0 commit comments

Comments
 (0)