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;
}
时间: 2024-10-26 22:50:15

poj 1080 (LCS变形)的相关文章

hdu 1080(LCS变形)

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

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(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 (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(动态规划)

一开始用的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

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

poj1080——lcs变形,dp

poj1080——lcs变形,dp Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17610   Accepted: 9821 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simpl

POJ 1458 LCS模板

LCS模板 存一个 #include "stdio.h" #include "string.h" int main() { char a[1010],b[1010]; int i,j,Max,dp[1010]; while (scanf("%s",a)!=EOF) { scanf("%s",b); memset(dp,0,sizeof(dp)); for (i=0;b[i];i++) { Max=0; for (j=0;a[j

【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