POJ 1080 Human Gene Functions 【dp】
题目大意:每次给出两个碱基序列(包含ATGC的两个字符串),其中每一个碱基与另一串中碱基如果配对或者与空串对应会有一个分数(可能为负),找出一种方式使得两个序列配对的分数最大
思路:字符串动态规划的经典题,很容易想到状态dp[i][j],指第一个长度为i的串和第二个长度为j的串配对的最大分数。显然,这个状态可以由dp[i][j-1],dp[i-1][j],dp[i-1][j-1]三个子问题得到,即第一串最后一个字符对应空格、第二串最后一个字符对应空格和第一串第二串最后一个字符配对所得到的分数这三者的最大值。
方程:dp[i][j]=max{dp[i-1][j]+grade[i][grade],dp[i][j-1]+grade[space][j],dp[i-1][j-1]+grade[i][j]}
grade[i][j]指的是字符串1的第i个碱基和字符串2的第j个碱基配对的分数 space指配对空格的分数
code:
#include<cstdio>
#include<string.h>
#include<iostream>
using namespace std;
const int trans[10][10]=
{{0},{0,5,-1,-2,-1,-3},
{0,-1,5,-3,-2,-4},
{0,-2,-3,5,-2,-2},
{0,-1,-2,-2,5,-1},
{0,-3,-4,-2,-1,-19941117}//我竟然和杜宇飞神犇生日同一天!!以后无穷大就用它了
};
int tr(char ch)
{
if (ch=='A')return 1;if (ch=='C')return 2;
if (ch=='G')return 3;if (ch=='T')return 4;
return 0;
}
int max(int a,int b,int c)
{
if (a<b)a=b;
if (a<c)a=c;
return a;
}
int main()
{
int t,gene1[200]={0},gene2[200]={0},n1,n2,dp[200][200]={{0}};
scanf("%d",&t);
char ch1[200],ch2[200];
for(int k=1;k<=t;k++)
{
memset(dp,0,sizeof(dp));
scanf("%d",&n1);
scanf("%s",ch1);
for(int i=1;i<=n1;i++)
{
gene1[i]=tr(ch1[i-1]);
}
scanf("%d",&n2);
scanf("%s",ch2);
for(int i=1;i<=n2;i++)
{
gene2[i]=tr(ch2[i-1]);
}
for(int i=1;i<=n1;i++)dp[i][0]=dp[i-1][0]+trans[gene1[i]][5];
for(int i=1;i<=n2;i++)dp[0][i]=dp[0][i-1]+trans[gene2[i]][5];
for(int i=1;i<=n1;i++)
{
for(int j=1;j<=n2;j++)
{
dp[i][j]=max(dp[i-1][j]+trans[gene1[i]][5],
dp[i][j-1]+trans[gene2[j]][5],
dp[i-1][j-1]+trans[gene1[i]][gene2[j]]);
}
}
printf("%d\n",dp[n1][n2]);
}
return 0;
}
调试结果:2WA 原因:一开始看discuss里别人犯错的原因:矩阵抄错、dp方程写错、数组写小了.....一个个对下来好像都没错,从头到尾看了两遍才发现dp的初始条件写错了,这样竟然都能过样例太坑了!!看来以后自己构造边缘数据测试程序鲁棒性的能力得加强!!
【BTW】貌似比较正规的解题报告就是从这篇开始的(正规是指题目大意,算法,code以及附加)
POJ 1080 Human Gene Functions 【dp】的更多相关文章
- poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17805 Accepted: ...
- poj 1080 Human Gene Functions(lcs,较难)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19573 Accepted: ...
- poj 1080 Human Gene Functions(dp)
题目:http://poj.org/problem?id=1080 题意:比较两个基因序列,测定它们的相似度,将两个基因排成直线,如果需要的话插入空格,使基因的长度相等,然后根据那个表格计算出相似度. ...
- POJ 1080 Human Gene Functions -- 动态规划(最长公共子序列)
题目地址:http://poj.org/problem?id=1080 Description It is well known that a human gene can be considered ...
- dp poj 1080 Human Gene Functions
题目链接: http://poj.org/problem?id=1080 题目大意: 给两个由A.C.T.G四个字符组成的字符串,可以在两串中加入-,使得两串长度相等. 每两个字符匹配时都有个值,求怎 ...
- hdu 1080 Human Gene Functions(DP)
题意: 人类基因由A.C.G.T组成. 有一张5*5的基因表.每格有一个值,叫相似度.例:A-C:-3.意思是如果A和C配对, 则它俩的相似度是-3[P.S.:-和-没有相似度,即-和-不能配对] 现 ...
- POJ 1080 Human Gene Functions
题意:给两个DNA序列,在这两个DNA序列中插入若干个'-',使两段序列长度相等,对应位置的两个符号的得分规则给出,求最高得分. 解法:dp.dp[i][j]表示第一个字符串s1的前i个字符和第二个字 ...
- poj 1080 Human Gene Functions (最长公共子序列变形)
题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...
- 【HDOJ】1080 Human Gene Functions
DP.wa了一下午,原来是把mmax写在外层循环了.最近事情太多了,刷题根本没状态. #include <cstdio> #include <cstring> #include ...
随机推荐
- AndroidStudio碰到的各种问题
源码已经下载了,但是为毛关联不了? 我的源码默认是下载在Sdk\sources\android-23\目录下面的,以前开发的时候都是自动关联的,今天碰到了怎么刷新,怎么关联都不行. 解决方式为: 1. ...
- 自动创建xml文档
自动创建xml文档 import xml.etree.ElementTree as ET print(dir(ET)) #ET里面有Element方法 root = ET.Element(" ...
- 严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:解决
严重 [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ContainerBase.addChildInternal Contain ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- (转)使用CGLIB实现AOP功能与AOP概念解释
http://blog.csdn.net/yerenyuan_pku/article/details/52864395 使用CGLIB实现AOP功能 在Java里面,我们要产生某个对象的代理对象,这个 ...
- Mathematics-基础:斐波那契数列
f(1)=1 f(2)=1 f(n)=f(n-1)+f(n-2) n>2
- ideal取消按下两次shift弹出搜索框 修改idea,webstrom,phpstrom 快捷键double shift 弹出search everywhere
因为经常需要在中英文之间切换,所以时常使用shift键,一不小心就把这个Searchwhere 对话框调出来了,很是麻烦. 因此痛定思痛, 我决定将这个按两下shift键就弹出搜索框的快捷键禁用了! ...
- myBatis的binding错误:Invalid bound statement (not found)
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误这个问题我找了好久,终于找到了正确的写 ...
- 剑指Offer(Python)
014-链表中倒数第k个结点 用快慢指针:p2比p1先走到k:间隔了k-1)步,然后再一起走,当p2为最后一个时,p1就为倒数第k个数 class ListNode: def __init__(sel ...
- merge dict key
#!/usr/local/python # -*- coding:utf-8 -*-user_dict = {'python': 23, 'Python': 51, '机器':10, 'PYTHON' ...