题目链接:

http://poj.org/problem?id=1080

题目大意:

给两个由A、C、T、G四个字符组成的字符串,可以在两串中加入-,使得两串长度相等。

每两个字符匹配时都有个值,求怎样安排使得总的值最大,两个-不能匹配。

解题思路:

这题转化一下就是一个裸的最长公共子串问题,只不过要求匹配时长度一样。

dp[i][j]表示第一串的第前i个字符和第二串的前j个字符匹配时,能达到的最大值。

初始化时注意dp[0][j]和dp[j][0]不能为零,为相应字符与-匹配时的总和。

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF 0x1f1f1f1f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; /*
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
*/
#define Maxn 110
map<char,int>myp;
int mar[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,-INF}}; //存入值表
char sa1[Maxn],sa2[Maxn];
int a[Maxn],b[Maxn];
int dp[Maxn][Maxn]; int main()
{
myp['A']=0,myp['C']=1,myp['G']=2,myp['T']=3,myp['-']=4; //将字符和表映射起来
int t,len1,len2; scanf("%d",&t);
while(t--)
{
scanf("%d%s",&len1,sa1+1);
for(int i=1;i<=len1;i++)
a[i]=myp[sa1[i]]; //转化成相应的代表数字
scanf("%d%s",&len2,sa2+1);
for(int i=1;i<=len2;i++)
b[i]=myp[sa2[i]]; memset(dp,-INF,sizeof(dp));
dp[0][0]=0;
for(int i=1;i<=len1;i++) //注意初始化时 是和-匹配
dp[i][0]=dp[i-1][0]+mar[a[i]][4];
// printf("i:%d j:%d %d\n",i,0,dp[i][0]);
for(int j=1;j<=len2;j++)
dp[0][j]=dp[0][j-1]+mar[b[j]][4];
//printf("i:%d j:%d %d\n",0,j,dp[0][j]);
// putchar('\n');
for(int i=1;i<=len1;i++)
for(int j=1;j<=len2;j++)
{
int Mx=dp[i][j];
Mx=max(Mx,dp[i-1][j-1]+mar[a[i]][b[j]]); //i-j匹配
Mx=max(Mx,max(dp[i-1][j]+mar[a[i]][4],dp[i][j-1]+mar[b[j]][4])); //(j和i-1,i和-)匹配 (i和j-1,-和j)匹配
Mx=max(Mx,dp[i-1][j-1]+mar[a[i]][4]+mar[b[i]][4]); //两个都匹配-
dp[i][j]=Mx;
//printf("i:%d j:%d %d\n",i,j,dp[i][j]);
}
printf("%d\n",dp[len1][len2]);
}
return 0;
}

dp poj 1080 Human Gene Functions的更多相关文章

  1. poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17805   Accepted:  ...

  2. poj 1080 Human Gene Functions(lcs,较难)

    Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19573   Accepted:  ...

  3. POJ 1080 Human Gene Functions -- 动态规划(最长公共子序列)

    题目地址:http://poj.org/problem?id=1080 Description It is well known that a human gene can be considered ...

  4. poj 1080 Human Gene Functions(dp)

    题目:http://poj.org/problem?id=1080 题意:比较两个基因序列,测定它们的相似度,将两个基因排成直线,如果需要的话插入空格,使基因的长度相等,然后根据那个表格计算出相似度. ...

  5. POJ 1080 Human Gene Functions 【dp】

    题目大意:每次给出两个碱基序列(包含ATGC的两个字符串),其中每一个碱基与另一串中碱基如果配对或者与空串对应会有一个分数(可能为负),找出一种方式使得两个序列配对的分数最大 思路:字符串动态规划的经 ...

  6. POJ 1080 Human Gene Functions

    题意:给两个DNA序列,在这两个DNA序列中插入若干个'-',使两段序列长度相等,对应位置的两个符号的得分规则给出,求最高得分. 解法:dp.dp[i][j]表示第一个字符串s1的前i个字符和第二个字 ...

  7. poj 1080 Human Gene Functions (最长公共子序列变形)

    题意:有两个代表基因序列的字符串s1和s2,在两个基因序列中通过添加"-"来使得两个序列等长:其中每对基因匹配时会形成题中图片所示匹配值,求所能得到的总的最大匹配值. 题解:这题运 ...

  8. hdu 1080 Human Gene Functions(DP)

    题意: 人类基因由A.C.G.T组成. 有一张5*5的基因表.每格有一个值,叫相似度.例:A-C:-3.意思是如果A和C配对, 则它俩的相似度是-3[P.S.:-和-没有相似度,即-和-不能配对] 现 ...

  9. P 1080 Human Gene Functions

    大概作了一周,终于A了 类似于求最长公共子序列,稍有变形 当前序列 ch1 中字符为 a,序列 ch2 中字符为 b 则有 3 种配对方式: 1. a 与 b 2. a 与 - 3. - 与 b 动态 ...

随机推荐

  1. Object-C — KVO & oc通知

    键值观察(KVO)是基于键值编码的一种技术. 利用键值观察可以注册成为一个对象的观察者,在该对象的某个属性变化时收到通知. 被观察对象需要编写符合KVC标准的存取方法,编写键值观察分为以下三步: (1 ...

  2. Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction policies

    Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction pol ...

  3. SQL Trainning 总结

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  4. .NET生成静态页面例子

    主要做法如下: 1.创建网站,并创建一个模板页,template.htm 2.添加一个web窗体Default.aspx 3.在网站下新建文件夹htm,设置该文件夹的属性,确保该文件夹具有可写权限 详 ...

  5. ffmpeg与RTMP流媒体连接用法(翻译) http://www.chinavideo.org/forum.php?mod=viewthread&tid=15423

    最近浏览国外网站时候发现,翻译不准确的敬请谅解. 1.将文件当做直播送至liveffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/liv ...

  6. UEditor配置图片上传

    最近项目中需要用到一个图文编辑器功能,因为之前的kingeditor功能过于简陋,所以决定换成Ueditor,前端已经配置好了,这个是后台配置 1,确定前台已经配置好了 2,将编辑器的插件包下载下来, ...

  7. 支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url.

    支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url. 现支付宝的通知有两类. A服务器通知,对应的参数为notify_url,支付宝通知使用POST方式 B页面跳转通 ...

  8. [Linux]Ubuntu下如何将普通用户提升到root权限

    转至:http://jingyan.baidu.com/album/6181c3e0780131152ef153ff.html?picindex=0&qq-pf-to=pcqq.c2c  在u ...

  9. MyEclipse10

    1.配置tomcat Windows->Preferences->My Eclipse->Servers->Tomcat,对于64位操作系统而言,Tomcat home dir ...

  10. css3实现钟表特效

    <!doctype html><html><head><meta http-equiv="Content-Type" content=&q ...