http://poj.org/problem?id=1080 (题目链接)

题意

  给出两个只包含字母ACGT的字符串s1、s2,可以在两个字符串中插入字符“-”,使得s1与s2的相似度最大。

Solution

  动态规划。

  用f[i][j]表示字符串s1前i位和s2前j位的最大相似度,转移很简单,直接看程序吧,边界条件要注意,当i=0或j=0时,就等于是在长度等于0的字符串中全部插入“-”,使得两字符串长度相等的相似度。打个表预处理出每两个字符的相似度比较方便后面的操作。

代码

// poj1080
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi 3.1415926535898
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; int f[110][110],w[510][510],T,n1,n2;
char s1[110],s2[110]; int main() {
scanf("%d",&T);
w['A']['A']=5;w['A']['C']=-1;w['A']['G']=-2;w['A']['T']=-1;w['A']['-']=-3;
w['C']['A']=-1;w['C']['C']=5;w['C']['G']=-3;w['C']['T']=-2;w['C']['-']=-4;
w['G']['A']=-2;w['G']['C']=-3;w['G']['G']=5;w['G']['T']=-2;w['G']['-']=-2;
w['T']['A']=-1;w['T']['C']=-2;w['T']['G']=-2;w['T']['T']=5;w['T']['-']=-1;
w['-']['A']=-3;w['-']['C']=-4;w['-']['G']=-2;w['-']['T']=-1;w['-']['-']=0;
while (T--) {
memset(f,0,sizeof(f));
scanf("%d%s%d%s",&n1,s1+1,&n2,s2+1);
f[0][0]=0;
for (int i=0;i<=n1;i++) f[i][0]=w[s1[i]]['-']+f[i-1][0];
for (int i=0;i<=n2;i++) f[0][i]=w['-'][s2[i]]+f[0][i-1];
for (int i=1;i<=n1;i++)
for (int j=1;j<=n2;j++) {
f[i][j]=f[i-1][j-1]+w[s1[i]][s2[j]];
f[i][j]=max(f[i][j],f[i-1][j]+w[s1[i]]['-']);
f[i][j]=max(f[i][j],f[i][j-1]+w['-'][s2[j]]);
}
printf("%d\n",f[n1][n2]);
}
return 0;
}

  

【poj1080】 Human Gene Functions的更多相关文章

  1. 【POJ 1080】 Human Gene Functions

    [POJ 1080] Human Gene Functions 相似于最长公共子序列的做法 dp[i][j]表示 str1[i]相应str2[j]时的最大得分 转移方程为 dp[i][j]=max(d ...

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

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

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

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

  4. Human Gene Functions

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

  5. hdu1080 Human Gene Functions() 2016-05-24 14:43 65人阅读 评论(0) 收藏

    Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  6. POJ 1080:Human Gene Functions LCS经典DP

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

  7. POJ 1080 Human Gene Functions 【dp】

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

  8. poj1080 - Human Gene Functions (dp)

    题面 It is well known that a human gene can be considered as a sequence, consisting of four nucleotide ...

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

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

随机推荐

  1. ExtJS要利用观察者模式 去实现自定义的事件

    // 要利用观察者模式 去实现自定义的事件 //1:由于浏览器他自己能定义内置的事件(click/blur...) // 我们也应该有一个类似于浏览器这样的类,这个类 自己去内部定义一些事件(自定义事 ...

  2. java10-2 toString()方法

    public String toString():返回该对象的字符串表示. Integer类下的一个静态方法: public static String toHexString(int i):把一个整 ...

  3. C# HttpWebRequest 绝技 转至 http://www.sufeinet.com/

    转至: 在线测试工具http://www.sufeinet.com/thread-3690-1-1.htmlc# HttpWebRequest与HttpWebResponse 绝技    如果你想做一 ...

  4. 一段后台C#查询SQL Server数据库代码

    using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.W ...

  5. SQL Server 阻止了对组件 'Ole Automation Procedures' 的 过程'sys.sp_OACreate' 的访问

    --开启 Ole Automation Procedures sp_configure ; GO RECONFIGURE; GO sp_configure ; GO RECONFIGURE; GO E ...

  6. JS面向对象的几种写法

    JS 中,面向对象有几种写法.归纳下,大概有下面这几种:工厂模式,构造函数模式,原型模式,构造函数与原型模式的混合使用,原型链继承,借用构造函数继承. 一.工厂模式 function person ( ...

  7. CSS3之firefox&safari背景渐变之争 - [前端技术][转]

    Firefox浏览器下的渐变背景  Firefox3.6background:-moz-linear-gradient(top, red, rgba(0, 0, 255, 0.5));chrome/S ...

  8. 实践SQLServer Tuning

    已有的系统业务数据属性多,表之间关系紧密.单表数据量(5481 row(s) affected)级别(其中三四个主表),其他表数据量较小. 0)使用set statistics生成辅助信息参考. se ...

  9. C语言操作符优先级

    C语言操作符优先级 优先级 运算符 含    义 要求运算 对象的个数 结合方向 1 () [] -> . 圆括号 下标运算符 指向结构体成员运算符 结构体成员运算符 自左至右 2 ! 逻辑非运 ...

  10. Java集合---HashSet的源码分析

    一.  HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持.它不保证set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用null元素. 二.  ...