poj1080--Human Gene Functions(dp:LCS變形),poj1080--humanlcs
Human Gene Functions
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 17206 |
|
Accepted: 9568 |
Description
It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their functions, because these can be used to diagnose human diseases and to design new drugs for them.
A human gene can be identified through a series of time-consuming biological experiments, often with the help of computer programs. Once a sequence of a gene is obtained, the next job is to determine its function.
One of the methods for biologists to use in determining the function of a new gene sequence that they have just identified is to search a database with the new gene as a query. The database to be searched stores many gene sequences and their functions – many researchers have been submitting their genes and functions to the database and the database is freely accessible through the Internet.
A database search will return a list of gene sequences from the database that are similar to the query gene.
Biologists assume that sequence similarity often implies functional similarity. So, the function of the new gene might be one of the functions that the genes from the list have. To exactly determine which one is the right one another series of biological experiments will be needed.
Your job is to make a program that compares two genes and determines their similarity as explained below. Your program may be used as a part of the database search if you can provide an efficient one.
Given two genes AGTGATG and GTTAG, how similar are they? One of the methods to measure the similarity
of two genes is called alignment. In an alignment, spaces are inserted, if necessary, in appropriate positions of
the genes to make them equally long and score the resulting genes according to a scoring matrix.
For example, one space is inserted into AGTGATG to result in AGTGAT-G, and three spaces are inserted into GTTAG to result in –GT--TAG. A space is denoted by a minus sign (-). The two genes are now of equal
length. These two strings are aligned:
AGTGAT-G
-GT--TAG
In this alignment, there are four matches, namely, G in the second position, T in the third, T in the sixth, and G in the eighth. Each pair of aligned characters is assigned a score according to the following scoring matrix.
denotes that a space-space match is not allowed. The score of the alignment above is (-3)+5+5+(-2)+(-3)+5+(-3)+5=9.
Of course, many other alignments are possible. One is shown below (a different number of spaces are inserted into different positions):
AGTGATG
-GTTA-G
This alignment gives a score of (-3)+5+5+(-2)+5+(-1) +5=14. So, this one is better than the previous one. As a matter of fact, this one is optimal since no other alignment can have a higher score. So, it is said that the
similarity of the two genes is 14.
Input
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each test case consists of two lines: each line contains an integer, the length of a gene, followed by a gene sequence. The length of each gene sequence is at least one and does not exceed 100.
Output
The output should print the similarity of each test case, one per line.
Sample Input
2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA
Sample Output
1421
Source
Taejon 2001 變形的lcs,和最長公用子序列的求法是一樣的,只是把長度+1,轉化為了分數的變化。注意:對於dp[0][i] , dp[i][0]這兩個位置的處理
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dp[120][120] , l1 , l2 ;char s1[120] , s2[120] ;char str[6] = "ACGT " ;int k[5][5] = { {5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,0} };int main(){ int t , i , j , ii , jj ; scanf("%d", &t); while(t--) { scanf("%d %s", &l1, s1); scanf("%d %s", &l2, s2); memset(dp,0,sizeof(dp)); for(i = 1 ; i <= l1 ; i++) { for(j = 0 ; j < 5 ; j++) if( str[j] == s1[i-1] ) break; dp[i][0] = k[j][4] + dp[i-1][0] ; } for(i = 1 ; i <= l2 ; i++) { for(j = 0 ; j < 5 ; j++) if( str[j] == s2[i-1] ) break; dp[0][i] = k[4][j] + dp[0][i-1] ; } for(i = 1 ; i <= l1 ; i++) { for(j =1 ; j <= l2 ; j++) { for(ii = 0 ; ii < 5 ; ii++) if( str[ii] == s1[i-1] ) break; for(jj = 0 ; jj < 5 ; jj++) if( str[jj] == s2[j-1] ) break; dp[i][j] = max( dp[i-1][j-1]+k[ii][jj],max(dp[i-1][j]+k[ii][4],dp[i][j-1]+k[4][jj]) ); } } printf("%d\n", dp[l1][l2]); } return 0;}
ACM解題報告
一個POJ題目分類
主流演算法:
1.搜尋 //回溯
2.DP(動態規劃)
3.貪心
4.圖論 //Dijkstra、最小產生樹、網路流
5.數論 //解模線性方程
6.計算幾何 //凸殼、同等安置矩形的並的面積與周長
7.組合數學 //Polya 定理
8.類比
9.資料結構 //並查集、堆
10.博弈論
1、 排序
1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376,
2377, 2380, 1318, 1877,1928, 1971, 1974, 1990, 2001, 2002, 2092, 2379,
1002(需要字元處理,排序用快排即可) 1007(穩定的排序) 2159(題意較難懂)
2231 2371(簡單排序) 2388(順序統計演算法) 2418(二叉排序樹)
2、 搜尋、回溯、遍曆
1022,1111,1118,1129,1190,1562,1564,1573,1655,2184 ,2225,2243,2312,2362,2378,2386,1010,1011,1018,1020,1054,1062,1256,1321,1363,1501,1650,1659,1664,1753,2078,2083,2303,2310,2329
簡單:1128, 1166, 1176, 1231, 1256, 1270, 1321, 1543, 1606, 1664,
1731, 1742, 1745, 1847,1915, 1950, 2038, 2157, 2182, 2183, 2381, 2386, 2426
不易:1024, 1054, 1117, 1167, 1708, 1746, 1775, 1878, 1903, 1966, 2046,
2197, 2349
推薦:1011, 1190, 1191, 1416, 1579, 1632, 1639, 1659, 1680, 1683, 1691,
1709, 1714, 1753,1771, 1826, 1855, 1856, 1890, 1924, 1935, 1948, 1979, 1980, 2170,2288, 2331, 2339,2340,1979(和迷宮類似),1980(對剪枝要求較高)
3、 曆法
1008 2080 (這種題要小心)
4、 枚舉
1012,1046, 1387, 1411, 2245, 2326, 2363, 2381,1054(剪枝要求較高),1650 (小數的精度問題)
5、 資料結構的典型演算法
容易:1182, 1656, 2021, 2023, 2051, 2153, 2227, 2236, 2247, 2352,
2395
不易:1145, 1177, 1195, 1227, 1661, 1834
推薦:1330, 1338, 1451, 1470, 1634, 1689, 1693, 1703, 1724, 1988, 2004,
2010, 2119, 2274
1125(弗洛伊德演算法) ,2421(圖的最小產生樹)
6、 動態規劃
1037 A decorative fence、
1050 To the Max、
1088 滑雪、
1125 Stockbroker Grapevine、
1141 Brackets Sequence、
1159 Palindrom......餘下全文>>