zoukankan      html  css  js  c++  java
  • 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(dp[i-1][j-1] + value(a[i],b[i]),dp[i][j-1] + value('-', b[i]), dp[i-1][j] + value(a[i],'-');

    边界:dp[i][0] = dp[i-1][0] + value(a[i],'-'),(i<=a.size);      dp[0][j] = dp[0][j-1] + value('-',b[i]), (j <= b.size);

    边界是个大问题!!

     a[i]跟dp[i]的关系搞错.跪了整整一个小时啊!!!

    #include <iostream>
    #include <algorithm>
    #include <stdlib.h>
    #include <time.h>
    #include <cmath>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    
    #define c_false ios_base::sync_with_stdio(false); cin.tie(0)
    #define INF 0x3f3f3f3f
    #define INFL 0x3f3f3f3f3f3f3f3f
    #define zero_(x,y) memset(x , y , sizeof(x))
    #define zero(x) memset(x , 0 , sizeof(x))
    #define MAX(x) memset(x , 0x3f ,sizeof(x))
    #define swa(x,y) {LL s;s=x;x=y;y=s;}
    using namespace std ;
    #define N 105
    
    const double PI = acos(-1.0);
    typedef long long LL ;
    char a[N],b[N];
    int dp[N][N];
    
    int value(char x, char y){
        if(x == y)return 5;
        else if((x == 'A'&&y == 'C')||(x == 'C'&&y == 'A'))return -1;
        else if((x == 'A'&&y == 'G')||(x == 'G'&&y == 'A'))return -2;
        else if((x == 'A'&&y == 'T')||(x == 'T'&&y == 'A'))return -1;
        else if((x == 'C'&&y == 'G')||(x == 'G'&&y == 'C'))return -3;
        else if((x == 'C'&&y == 'T')||(x == 'T'&&y == 'C'))return -2;
        else if((x == 'T'&&y == 'G')||(x == 'G'&&y == 'T'))return -2;
        else if((x == 'A'&&y == '-')||(x == '-'&&y == 'A'))return -3;
        else if((x == 'C'&&y == '-')||(x == '-'&&y == 'C'))return -4;
        else if((x == 'G'&&y == '-')||(x == '-'&&y == 'G'))return -2;
        else if((x == 'T'&&y == '-')||(x == '-'&&y == 'T'))return -1;
        else return 0;
    }
    int main(){
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n,aL,bL;
        cin>>n;
        while(n--){
            cin>>aL;
            scanf("%s",a);
            cin>>bL;
            scanf("%s",b);
            dp[0][0] = 0;
            for(int i = 0;i < aL; i++) {dp[i+1][0] = dp[i][0] + value(a[i],'-');}
            for(int i = 0;i < bL; i++) {dp[0][i+1] = dp[0][i] + value(b[i],'-');}
            for(int i = 1;i <= aL; i++){
                for(int j = 1;j <= bL; j++){
                    if(a[i-1] == b[j-1]) dp[i][j] = dp[i-1][j-1] + value(a[i-1], b[j-1]);
                    else dp[i][j] = max(dp[i-1][j-1]+value(a[i-1],b[j-1]), max(dp[i][j-1]+value('-',b[j-1]), dp[i-1][j]+value(a[i-1],'-')));
                }
            }
            cout<<dp[aL][bL]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Android见招拆招五:XML匹配问题、XML资源引用的必要性
    Android见招拆招四:Manifest.xml内容神秘还原
    Android学习笔记三:Intent实现页面跳转
    Android学习笔记二:(布局)Linear Layout、Layout weight测试
    Android见招拆招三:Eclipse软件误报
    Android学习笔记一:(布局)fill_parent、wrap_content、match_parent实例测试
    文件读写(笔记)
    常用的异常
    面向过程、函数式、面向对象
    time&datetime&random模块
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/5285182.html
Copyright © 2011-2022 走看看