POJ1080Human Gene Functions(LCS变形)

题目链接

题目:给出两个串,每匹配一种有一种权值,求权值最大的匹配串

就是 最长公共子序列的 的思想: 首先对于 i 和 j 来比较, 一种情况是i和j匹配,此时 dp[i][j] = dp[i - 1][j - 1] + g[ str1[i] ][ str2[j] ],另一种情况是i和j不匹配,那么就有两种情况,一 i 和 j前面的匹配,j与一个空 即 ‘ - ’匹配,dp[i][j] = dp[i ][ j - 1] + g[ ‘ - ‘ ][ str2[j] ] ,二 i 前面的 和 j匹配上,此时 i 和 ‘ - ’匹配,dp[i][j] = dp[i - 1][ j] + g[ str1[i] ][ ‘-‘ ],三种情况取最大。

这题就败在了初始化上=_=

第一次只初始化了dp[0][0],后来将 dp[1][0]和dp[0][1]又初始化了,其实要把 dp【0】【i] 和 dp[i][0] 和 dp【0][0] 都要初始化,这也是很明显的=_=

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <map>
 5 #include <algorithm>
 6 using namespace std;
 7 const int INF = 0x3f3f3f3f;
 8 const int Max = 210;
 9 int g[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, INF} };
10 map<char, int> m; // 为每一个 字母建立一个映射
11 int dp[Max][Max];
12 int main()
13 {
14     m[‘A‘] = 0;
15     m[‘C‘] = 1;
16     m[‘G‘] = 2;
17     m[‘T‘] = 3;
18     m[‘-‘] = 4;
19     int T;
20     scanf("%d", &T);
21     while (T--)
22     {
23         int len1, len2;
24         char str1[Max], str2[Max];
25         scanf("%d %s", &len1, str1 + 1);
26         scanf("%d %s", &len2, str2 + 1);
27         dp[0][0] = 0 ;
28         for (int i = 1; i <= len2; i++)
29             dp[0][i] = dp[0][i - 1] + g[4][ m[str2[i]] ];
30         for (int i = 1; i <= len1; i++)
31             dp[i][0] = dp[i - 1][0] + g[ m[str1[i]] ][4];
32
33         //cout << str1 + 1 << endl;
34         //cout << str2 + 1 << endl;
35         for (int i = 1; i <= len1; i++)
36         {
37             for (int j = 1; j <= len2; j++)
38             {
39                 dp[i][j] = dp[i - 1][j - 1] + g[ m[str1[i]] ][ m[str2[j]] ];
40                 //if (dp[i - 1][j] + g[ m[str1[i]] ][4]  INF)
41                 dp[i][j] = max(dp[i][j], dp[i - 1][j] + g[ m[str1[i]] ][4]); //其实初始化在这里很明显,因为要用dp[i - 1][j】,当 i= 1时,必须知道所有的 dp[0][j]+_+
42                 //if (dp[i][j - 1] + g[4][ m[str2[j]] ] != INF)
43                 dp[i][j] = max(dp[i][j], dp[i][j - 1] + g[4][ m[str2[j]] ]);
44             }
45         }
46         printf("%d\n", dp[len1][len2]);
47     }
48     return 0;
49 }

时间: 2024-12-31 20:45:25

POJ1080Human Gene Functions(LCS变形)的相关文章

poj1080--Human Gene Functions(dp:LCS变形)

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

POJ - 1080 - Human Gene Functions (LCS的变形)

题目传送:Human Gene Functions 思路:LCS的变形,定义状态dp[ i ][ j ]为取字符串s前i个字符字符串t前j个字符所获得的最大值,则可以得到状态转移方程为: dp[ i ][ j ] = max(dp[ i ][ j - 1] + f[ ' - ' ][ t[ j ] ], dp[ i - 1 ][ j ] + f[ s [ i ] ][ ' - ' ], dp[i - 1][ j - 1] + f[ s [ i ] ][ t [ j ] ]); AC代码: #in

POJ 1080:Human Gene Functions LCS经典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

POJ1080 Human Gene Functions 动态规划 LCS的变形

题意读了半年,唉,给你两串字符,然后长度不同,你可以用'-'把它们补成相同长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分 跟LCS很像的DP数组 dp[i][j]表示第一个字符串取第i个元素第二个字符串取第三个元素,然后再预处理一个得分表加上即可 得分表: score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5; score['A']['C'] = score['C']['A'] = -1;

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的

hdoj-1503-Human Gene Functions【LCS的变形】

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

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

poj 1080 Human Gene Functions(lcs,较难)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19573   Accepted: 10919 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 (LCS变形)

Human Gene Functions 题意: LCS: 设dp[i][j]为前i,j的最长公共序列长度: dp[i][j] = dp[i-1][j-1]+1;(a[i] == b[j]) dp[i][j] = max(dp[i][j-1],dp[i-1][j]); 边界:dp[0][j] = 0(j<b.size) ,dp[i][0] = 0(i< a.size); LCS变形: 设dp[i][j]为前i,j的最大价值: value(x, y)为比较价值: dp[i][j] = max(d