HDU 1080 Human Gene Functions

最长公共子序列的变形

题目大意:给出两个基因序列,求这两个序列的最大相似度。

题目中的表格给出了两两脱氧核苷酸的相似度。

状态转移方程为:

dp[i][j] = max(dp[i-1][j]+Similarity(s1[i], ‘-‘),
                     dp[i][j-1]+Similarity(s2[j], ‘-‘),
                     dp[i-1][j-1]+Similarity(s1[i], s2[j]));

注意边界的初始化。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 char s1[105], s2[105];
 9 int dp[105][105];
10
11 int table[5][5] = {
12      5, -1, -2, -1, -3,
13     -1,  5, -3, -2, -4,
14     -2, -3,  5, -2, -2,
15     -1, -2, -2,  5, -1,
16     -3, -4, -2, -1,  0
17 };
18
19 int max(int a, int b, int c)
20 {
21     return max(max(a, b), c);
22 }
23
24 int f(char c)
25 {
26     if(c == ‘A‘)    return 0;
27     if(c == ‘C‘)    return 1;
28     if(c == ‘G‘)    return 2;
29     if(c == ‘T‘)    return 3;
30     if(c == ‘-‘)    return 4;
31 }
32
33 int Similarity(char c1, char c2)
34 {    return table[f(c1)][f(c2)];    }
35
36 int main(void)
37 {
38     #ifdef LOCAL
39         freopen("1080in.txt", "r", stdin);
40     #endif
41
42     int T;
43     scanf("%d", &T);
44     while(T--)
45     {
46         int len1, len2, i, j;
47         dp[0][0] = 0;
48         scanf("%d %s", &len1, s1+1);
49         scanf("%d %s", &len2, s2+1);
50         for(i = 1; i <= len1; ++i)
51             dp[i][0] = dp[i-1][0] + Similarity(s1[i], ‘-‘);
52         for(i = 1; i <= len2; ++i)
53             dp[0][i] = dp[0][i-1] + Similarity(s2[i], ‘-‘);
54         for(i = 1; i <= len1; ++i)
55             for(j = 1; j <= len2; ++j)
56                 dp[i][j] = max(dp[i-1][j]+Similarity(s1[i], ‘-‘),
57                                 dp[i][j-1]+Similarity(s2[j], ‘-‘),
58                                 dp[i-1][j-1]+Similarity(s1[i], s2[j]));
59
60         printf("%d\n", dp[len1][len2]);
61     }
62     return 0;
63 }

代码君

HDU 1080 Human Gene Functions

时间: 2024-11-11 03:05:15

HDU 1080 Human Gene Functions的相关文章

hdu 1080 Human Gene Functions (动态规划)

Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2402    Accepted Submission(s): 1360 Problem Description It is well known that a human gene can be considered as a sequence,

hdu 1080 Human Gene Functions(DP)

题意: 人类基因由A.C.G.T组成. 有一张5*5的基因表.每格有一个值,叫相似度.例:A-C:-3.意思是如果A和C配对, 则它俩的相似度是-3[P.S.:-和-没有相似度,即-和-不能配对] 现在给两条基因片段.(长度不一定相等) 现在你要在两条基因片段中插入若干个-(空白基因),使得两个基因片段长度相等,且得到的整体相似度的值最大.[再次P.S.:-和-不能配对] 思路: 因为-和-不能匹配,所以插入的-的个数是有限的. str1的第一个基因可以与str1的第一个或-配对.然后,,,,很

POJ 1080 Human Gene Functions(LCS)

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

P 1080 Human Gene Functions

大概作了一周,终于A了 类似于求最长公共子序列,稍有变形 当前序列 ch1 中字符为 a,序列 ch2 中字符为 b 则有 3 种配对方式: 1. a 与 b 2. a 与 - 3. - 与 b 动态转移方程: dp[i][j] = max(dp[i - 1][j - 1] + g(ch1[i],ch2[j]) , dp[i - 1][j] + g(ch1[i],'-') , dp[i][j-1] + g('-',ch2[j])) 代码如下: #include<stdio.h> #includ

POJ #1080 - Human Gene Functions

A classic 2D DP problem. A disguise of LCS - actually not very hard to decode: it is about 2 sequences' matching, though with a weight value of each match. The point of this problem: how to decode problem statement and how to distill the actuall mode

POJ 1080 Human Gene Functions(动态规划)

一开始用的DFS,无限TLE,贴丑代码 //version 1 TLE #include<cstdio> #include<cstring> #include<iostream> #define MAX_INT 2147483647 #define MAXN 105 using namespace std; int Map[5][5] = { {0,-3,-4,-2,-1}, {-3,5,-1,-2,-1}, {-4,-1,5,-3,-2}, {-2,-2,-3,5,-

POJ 1080 Human Gene Functions(求两字符串相似度:LCS变形)

POJ 1080 Human Gene Functions(求两字符串相似度:LCS变形) http://poj.org/problem?id=1080 题意: HDU1080 给你两个由字符A,C,G,T构造的字符串s1和s2, 现在你可以在这两个字符串中插入空格, 使得两串长相等(但是不能使得s1的空格对应s2的空格位置). 然后给你s1的特定字符对应s2中特定字符所能获得的分数矩阵: 问你最后两个字符串所能获得的最大分数是多少? 分析: 本题很类似于求字符串最短编辑距离或者求字符串LCS的

POJ 1080 Human Gene Functions(DP)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18007   Accepted: 10012 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

poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17805   Accepted: 9917 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