Question: each time two base sequences are given (including two strings of atgc). If each base is paired with the base in the other string or corresponds to an empty string, there will be a score (which may be negative ), find a way to maximize the scores of pairing two sequences
Idea: typical question about string dynamic planning, it is easy to think of status DP [I] [J], the maximum score of pairing the first string with I and the second string with J. Obviously, this state can be obtained by DP [I] [J-1], DP [I-1] [J], DP [I-1] [J-1, that is, the maximum value of the first character corresponds to a space, the last character in the second string corresponds to a space, and the last character in the second string matches the second character.
Equation: DP [I] [J] = max {DP [I-1] [J] + grade [I] [Grade], DP [I] [J-1] + grade [space] [J], DP [I-1] [J-1] + grade [I] [J]}
Grade [I] [J] refers to the fraction space of the I-th base of string 1 and the J-th base pair of string 2. It refers to the score of the paired space.
Code:
# Include <cstdio>
# Include <string. h>
# Include <iostream>
Using namespace STD;
Const int trans [10] [10] =
{0}, {0, 5,-1,-2,-1,-3 },
{0,-1, 5,-3,-2,-4 },
{0,-2,-3, 5,-2,-2 },
{0,-1,-2,-2, 5,-1 },
{0,-3,-4,-2,-1,-19941117} // my birthday is the same as du yufei's birthday !! It will be used in the future with infinity.
};
Int tr (char ch)
{
If (CH = 'A') return 1; if (CH = 'C') return 2;
If (CH = 'G') return 3; if (CH = 'T') return 4;
Return 0;
}
Int max (int A, int B, int C)
{
If (A <B) A = B;
If (A <c) A = C;
Return;
}
Int main ()
{
Int T, gene1 [200] = {0}, gene2 [200] = {0}, N1, N2, DP [200] [200] = {0 }};
Scanf ("% d", & T );
Char limit [200], CH2 [200];
For (int K = 1; k <= T; k ++)
{
Memset (DP, 0, sizeof (DP ));
Scanf ("% d", & N1 );
Scanf ("% s", callback );
For (INT I = 1; I <= N1; I ++)
{
Gene1 [I] = tr (I-1);
}
Scanf ("% d", & N2 );
Scanf ("% s", CH2 );
For (INT I = 1; I <= n2; I ++)
{
Gene2 [I] = tr (CH2 [I-1]);
}
For (INT I = 1; I <= N1; I ++) DP [I] [0] = DP [the I-1] [0] + trans [gene1 [I] [5];
For (INT I = 1; I <= n2; I ++) DP [0] [I] = DP [0] [I-1] + trans [gene2 [I] [5];
For (INT I = 1; I <= N1; I ++)
{
For (Int J = 1; j <= n2; j ++)
{
DP [I] [J] = max (DP [I-1] [J] + trans [gene1 [I] [5],
DP [I] [J-1] + trans [gene2 [J] [5],
DP [I-1] [J-1] + trans [gene1 [I] [gene2 [J]);
}
}
Printf ("% d \ n", DP [N1] [n2]);
}
Return 0;
}
Debugging result: 2wa cause: At the beginning, let's look at the reasons for mistakes made by others in discuss: matrix CC error, DP equation error, array write error ..... right one by one. After reading it twice from start to end, I found that the initial DP condition was wrong. In this way, it would be too difficult to pass the example !! It seems that the robustness of self-built Edge Data Testing programs will be enhanced in the future !!
[BTW] it seems that the formal problem-solving report starts from this article (Formal refers to the general idea of the question, algorithm, code, and additional)
Poj 1080 human gene functions [DP]