Luo gu 1140 similar gene
Address: http://www.luogu.org/problem/show?pid=1140
Topic background
As we all know, genes can be thought of as a base pair sequence. It contains 4 kinds of nucleotides, précis-writers as a,c,g,t. Biologists are working to find the function of human genes for the diagnosis of diseases and the invention of drugs. In the task of a human genome working group, biologists studied the similarity of two genes. Because this research has an unusual effect on the treatment of diseases.
Title Description
The similarity of two genes is calculated as follows:
For two known genes, such as AGTGATG and Gttag, their bases correspond to each other. Of course, some empty bases can be added to the middle--for example:
Thus, the similarity between the two genes can be described by the sum of the similarity between the bases, and the similarity between the bases is shown in the following table:
So the similarity is: (-3) +5+5+ (-2) + (-3) +5+ (-3) +5=9. Because the corresponding methods of the two genes are not unique, for example:
The similarity is: (-3) +5+5+ (-2) +5+ (-1) +5=14. The similarity of two genes is defined as the one with the greatest similarity among all correspondence methods.
Input/output format
Input format:
A total of two lines. Each line is first an integer representing the length of the gene, followed by a sequence of genes, with only a,c,g,t four letters in the sequence. The length of the 1<= sequence is <=100.
Output format:
Only one row, that is, the similarity of the input genes.
Input/Output sample
Input Sample # #:
7 AGTGATG
5 Gttag
Sample # # of output:
14
Ideas
linear DP.
Can be likened to the maximum common subsequence. Set D[I][J] to the maximum similarity to the J of the I and B strings of a string. Then there is the transfer equation:
D[i][j]=max{d[i-1][j-1]+cost (A[i],b[j]), D[i][j-1]+cost ('-', b[j]), D[i-1][j]+cost (A[i], '-')}
Note that initialize D[0][J] and d[i][0], the other string to the right one read into the convenience of processing.
Code
1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 Const intMAXN = -+Ten;6 Const intw[5][5]={ 7{5,-1,-2,-1,-3},8{-1,5,-3,-2,-4},9{-2,-3,5,-2,-2},Ten{-1,-2,-2,5,-1}, One{-3,-4,-2,-1,-1<< -} A }; - inth[333],D[MAXN][MAXN]; - intn,m; the CharA[MAXN],B[MAXN]; - -InlineintCostCharXChary) { - returnW[h[x]][h[y]]; + } - + intMain () { Ah['A']=0; h['C']=1; h['G']=2; h['T']=3; h['-']=4; at -scanf"%d%s%d%s", &n,a+1, &m,b+1); - for(intI=1; i<=n;i++) {d[i][0]=d[i-1][0]+cost (A[i],'-'); } - for(intj=1; j<=m;j++) {d[0][j]=d[0][j-1]+cost ('-', B[j]); } - - for(intI=1; i<=n;i++) in for(intj=1; j<=m;j++){ -d[i][j]=d[i-1][j-1]+Cost (A[i],b[j]); toD[i][j]=max (d[i][j],d[i-1][j]+cost (A[i],'-')); +D[i][j]=max (d[i][j],d[i][j-1]+cost (B[j],'-')); - } theprintf"%d", D[n][m]); * return 0; $}
Luo gu 1140 similar gene