Source: poj 1080 human gene functions
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1080
Solution Type: Dynamic Planning
Solution:
An online problem-solving report: the report is detailed:
Http://nash250.blog.hexun.com/9794371_d.html
And 《AlgorithmThe examples of LCS (longest common subsequence Longest Common subsequence) in the dynamic programming section in introduction are basically the same. They are used to test the similarity between two strings of genes, the difference is that in the introduction, LCS is used to indicate the similarity between two bases. The longer the LCS, the higher the similarity. In this topic, the value table of the degree of similarity between two bases is shown in the following table. The higher the sum of the matched values of each pair, the greater the degree of similarity.
First, let's look at the LCS solution,
Set the two bases to an, BM
A [I], B [J] respectively represent the I core of string
J core of string B
Ax, by is an, BM sub-string;
C [x] [Y] indicates the length of the oldest sequence between the substrings ax and.
Then C [N] [m] indicates the length of the oldest sequence between an and BM strings.
There is a state transition equation as follows:
C [I] [J] = C [I-1] [J-1] + 1 if I, j> 0 A [I] = B [J]
C [I] [J] = max (C [I] [J-1], C [I-1] [J]) if I, j> 0 A [I] ≈ [J]
Think about the boundary condition of the dynamic gauge equation:
For I = 0 to n do C [I, 0] else 0
For J = 0 to n do C [0, J] defaults 0
That is, the maximum length of each sub-sequence and string with a length of 0 is 0.
This is the solution of LCS.
In response to this question, the LCS state equation is slightly modified,
Set the two bases to an, BM
A [I], B [J] respectively represent the I core of string
J core of string B
Ax, by is an, BM sub-string;
C [x] [Y] indicates the maximum similarity between substrings ax and.
C [N] [m] indicates the maximum similarity between strings an and BM.
Value (x, y) indicates the degree of similarity between the two sides.
'-' Indicates that the core is empty.
There is a state transition equation as follows:
C [I] [J] = max (C [I-1] [J-1] + value (A [I], B [J]), c [I] [J-1] + value ('-', B [J]), C [I-1] [J] + value (A [I], '-')
If I, j> 0 A [I] =[ J]
Think about the boundary condition of the dynamic gauge equation:
For I = 0 to n do C [I] [0] limit 0
For J = 0 to n do C [0] [J] limit 0
That is, the similarity between each sub-sequence and an empty string is 0.
The ideas for this question are clear.
Submission:
Wa twice: no critical information is clearly analyzed.
Note:
Consider critical conditions.
SourceProgram:
# Include <iostream>
Using namespace STD;
Long a [110], B [110], V [110] [110];
Long R [] [5] = {0,-3,-4,-2,-1,-3,-1,-2,-1,-4, -,-3,-2,-2,-2,-3, 5,-2,-1,-1,-2 };
Long change (char C)
{
Switch (c)
{
Case 'A': return 1;
Case 'C': return 2;
Case 'G': return 3;
Case 'T': return 4;
}
}
Int main ()
{
Long casenum, Alen, blen, I, j, TMP, all, aonly, bonly;
Char C;
Cin> casenum;
While (casenum --)
{
Cin> Alen;
For (I = 1; I <= Alen; I ++)
{
Cin> C;
A [I] = change (C );
}
Cin> blen;
For (I = 1; I <= blen; I ++)
{
Cin> C;
B [I] = change (C );
}
V [0] [0] = 0;
For (I = 1; I <= Alen; I ++) V [I] [0] = R [A [I] [0] + V [I-1] [0];
For (I = 1; I <= blen; I ++) V [0] [I] = R [0] [B [I] + V [0] [I-1];
// The critical condition is the value obtained from the current vertex and,
// Accumulate with the previous vertex, not just the value obtained from the current vertex
For (I = 1; I <= Alen; I ++)
For (j = 1; j <= blen; j ++)
{
All = V [I-1] [J-1] + R [A [I] [B [J];
Aonly = V [I-1] [J] + R [A [I] [0];
Bonly = V [I] [J-1] + R [0] [B [J];
TMP = aonly> bonly? Aonly: bonly;
V [I] [J] = All> TMP? ALL: TMP;
}
Cout <V [Alen] [blen] <Endl;
}
Return 0;
}