【POJ 1080】 Human Gene Functions

【POJ 1080】 Human Gene Functions

类似于最长公共子序列的做法

dp[i][j]表示 str1[i]对应str2[j]时的最大得分

转移方程为

dp[i][j]=max(dp[i-1][j-1]+score[str1[i]][str2[j]],

max(dp[i-1][j]+score[str1[i]][‘-‘],dp[i][j-1]+score[‘-‘][str2[j]]) )

注意初始化0下标就好

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int dp[101][101];
char a[102],b[102];
int sc[‘Z‘+1][‘Z‘+1];

int main()
{
    int t,aa,bb,i,j;
    scanf("%d",&t);
    sc[‘A‘][‘A‘] = sc[‘C‘][‘C‘] = sc[‘G‘][‘G‘] = sc[‘T‘][‘T‘] = 5;
    sc[‘A‘][‘C‘] = sc[‘C‘][‘A‘] = sc[‘A‘][‘T‘] = sc[‘T‘][‘A‘] = sc[‘T‘][‘-‘] = -1;
    sc[‘G‘][‘A‘] = sc[‘A‘][‘G‘] = sc[‘C‘][‘T‘] = sc[‘T‘][‘C‘] = sc[‘G‘][‘T‘] = sc[‘T‘][‘G‘] = sc[‘G‘][‘-‘] = -2;
    sc[‘A‘][‘-‘] = sc[‘C‘][‘G‘] = sc[‘G‘][‘C‘] = -3;
    sc[‘C‘][‘-‘] = -4;
    while(t--)
    {
        scanf("%d %s %d %s",&aa,a+1,&bb,b+1);
        dp[0][0] = 0;
        for(i = 1; i <= bb; ++i) dp[0][i] = sc[b[i]][‘-‘] + dp[0][i-1];
        for(i = 1; i <= aa; ++i) dp[i][0] = sc[a[i]][‘-‘] + dp[i-1][0];
        for(i = 1; i <= aa; ++i)
        {
            for(j = 1; j <= bb; ++j)
            {
               dp[i][j] = max(dp[i-1][j-1] + sc[a[i]][b[j]],max(dp[i-1][j] + sc[a[i]][‘-‘],dp[i][j-1] + sc[b[j]][‘-‘]));
            }
        }
        printf("%d\n",dp[aa][bb]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-14 15:13:59

【POJ 1080】 Human Gene Functions的相关文章

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

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

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

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(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 - 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