poj 1080 dp

基因配对 给出俩基因链和配对的值  求配对值得最大值  简单dp

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxa = 205;
const int mina = -100000000;
char str1[maxa], str2[maxa];
int num[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,-10000000
};
int a1[maxa], a2[maxa];
int dp[maxa][maxa];
int main(){
   //freopen("in.cpp", "r", stdin);
    int t, n1, n2;
    scanf("%d", &t);
    while(t--){
        str1[0] = 4;
        str2[0] = 4;
        scanf("%d%s", &n1, str1+1);
        scanf("%d%s", &n2, str2+1);//printf("*");

        for(int i = 1; i <= n1; i++){
           // printf("%c", str1[i]);
            if(str1[i] == ‘A‘)
                str1[i] = 0;
            else if(str1[i] == ‘C‘)
                str1[i] = 1;
            else if(str1[i] == ‘G‘)
                str1[i] = 2;
            else str1[i] = 3;
        }
        for(int i = 1; i <= n2; i++){//printf("%d\n", i);
            if(str2[i] == ‘A‘)
                str2[i] = 0;
            else if(str2[i] == ‘C‘)
                str2[i] = 1;
            else if(str2[i] == ‘G‘)
                str2[i] = 2;
            else str2[i] = 3;
        }//printf("*");
        for(int i = 1; i <= n1; i++){
            if(i == 0)
                a1[i] = num[str1[i]][4];
            else a1[i] = num[str1[i]][4] + a1[i-1];
        }
        for(int i = 1; i <= n2; i++){
            if(i == 0)
                a2[i] = num[str2[i]][4];
            else a2[i] = num[str2[i]][4] + a2[i-1];
        }
        /*for(int i =0; i <= n1; i++){
            printf("%d ", a1[i]);
        }puts("");*/
        for(int i = 0;i  <= n1; i++){
            for(int k = 0; k <= n2; k++){
                dp[i][k] = mina;
            }
        }
        for(int i = 0; i <= n2; i++){
            if(i == 0)
                dp[1][i] = num[str1[1]][str2[i]];
            else
                dp[1][i] = num[str1[1]][str2[i]] + a2[i-1]-a2[0];
        }
        for(int i = 2; i <= n1; i++){
            dp[i][0] = a1[i] - a1[0];
            for(int k = 1; k <= n2; k++){
                dp[i][k] = max(dp[i][k], dp[i-1][k]+ num[str1[i]][4]);
                for(int j = 0; j < k; j++){
                    dp[i][k] = max(dp[i][k],dp[i-1][j] + num[str1[i]][str2[k]] + a2[k-1] - a2[j]);
                }
            }
        }
        /*for(int i = 1; i <= n1; i++){
            printf("*%d ", num[str1[i]][str2[1]]+a1[i-1]-a1[0]);
        }puts("");;
        for(int i = 0; i <= n1; i++){
            for(int k = 0; k <= n2; k++){
                printf("%d%d %d ",str1[i], str2[k], dp[i][k]);
            }puts("");
        }*/
        int ans = mina;
        for(int i = 0; i <= n2; i++){
            ans = max(ans, dp[n1][i] + a2[n2] - a2[i]);
           // printf("%d ", dp[n1][i] + a2[n2] - a2[i]);
        }//puts("");
        for(int i = 0; i <= n1; i++){
            ans = max(ans, dp[i][n2]+a1[n1] - a1[i]);
            //printf("%d ", dp[i][n2]+a1[n1] - a1[i]);
        }//puts("");
        printf("%d\n", ans);
    }

}

时间: 2024-08-09 19:44:37

poj 1080 dp的相关文章

poj 1080 Human Gene Functions (dp,LCS)

链接:poj 1080 题意:给定两个字符串,求它们对齐匹配的最大值 要求:可以两个字符匹配,也可以一个字符和'-'匹配, 但是不能两个'-'匹配,例如: AGTGATG GTTAG 这两个字符串可以看成是 AGTGATG -GTTA-G 也可以看成是 AGTGAT-G -GT--TAG 分析:这是一个变形的最长公共子序列,最优解: 1.取字符i-1和j-1的时候dp[i][j]=dp[i-1][j-1]+a[s1[i-1]][s2[j-1]]; 2.取字符i-1,不取j-1的时候dp[i][j

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

[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> #inclu

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

POJ 3670 &amp;&amp; POJ 3671 (dp)

最长不下降子序列的应用嘛.两题都是一样的. POJ 3670:求给定序列按递增或递减排列时,所需改变的最小的数字的数目. POJ 3671:求给定序列按递增排列时,所需改变的最小的数字的数目. 思路就是求最长不下降子序列,然后剩下的就是需要改变的字母. 最长不下降子序列:(我之前有写过,不懂请戳)http://blog.csdn.net/darwin_/article/details/38360997 POJ 3670: #include<cstdio> #include<cstring

poj 3783 DP 2个鸡蛋扔100层楼的加强版

http://poj.org/problem?id=3783 估计23号之后的排位赛之后我就要退役了,这之前最后再做5天ACM 今天的排位很惨,上次排位也很惨......这道题原来算法课老师讲过,模模糊糊记得方程,但是边界处理有问题, dp[i][j]=min(1+max(dp[k-1][j-1],dp[i-k][j]))   k=1 to 楼数 dp[i][j]:i层楼扔,手里有j个ball 的次数 边界两个:1.dp[1][i]=1,第一层无论手里有几个鸡蛋都是1次,2.dp[i][1]=i