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);
    }

}
View Code

 

posted @ 2014-12-26 21:37  icodefive  阅读(159)  评论(0编辑  收藏  举报