题意:求出将两个字符串改成一样长度所能形成最大的相似度。

  思路:这个可以说是编辑距离的一个变形,编辑距离最终状态时要两个字符串完全一致,这个就是要求长度一样,而且这个只允许插入“—”这一个字符。模仿编辑距离定义状态,dp[i][j]表示将第一个字符串的前i个字符与第二个字符串的前j个字符变为相同长度所能形成的最大相似度。设两个字母的相似度为g[i][j];

  那状态转移为 dp[i][j] = max( dp[i][j-1] + g[j][5], d[i-1][j] + g[i][5],dp[i-1][j-1] + g[i][j] ) 前两个代表插入“—”,后一个表示直接匹配。

  注意:这个题我WA了两次,在定义初始状态dp[i][0] 和 dp[0][i]时,我忘记了累加消耗,而且还过了样例……后来自己举了一个例子才修正的。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
#define N 220
map<char,int> m;
int g[N][N];
void Init();///初始化得出相似度
char a[N],b[N];
int Solve(int lena,int lenb){
int dp[N][N];
char tmpa,tmpb;
int numa,numb,sum;
memset(dp,,sizeof(dp));
sum = ;
for(int i = ;i <= lenb;i++){
tmpb = b[i];
numb = m[tmpb];
sum += g[][numb];///不要忘记消耗是累加的
dp[][i] = sum;
}
sum = ;
for(int i = ;i <= lena;i++){
tmpa = a[i];
numa = m[tmpa];
sum += g[numa][];
dp[i][] = sum;
}
for(int i = ;i <= lena;i++){
for(int j = ;j <= lenb;j++){
tmpa = a[i],tmpb = b[j];
numa = m[tmpa],numb = m[tmpb];
dp[i][j] = max(dp[i-][j]+g[numa][],dp[i][j-]+g[][numb]);
dp[i][j] = max(dp[i][j],dp[i-][j-]+g[numa][numb]);
}
}
return dp[lena][lenb];
}
void Input(){
int t,n,mm;
scanf("%d",&t);
while(t--){
scanf("%d %s",&n,a+);
scanf("%d %s",&mm,b+);
printf("%d\n",Solve(n,mm));
}
}
int main(){
// freopen("A.in.cpp","r",stdin);
Init();
Input();
return ;
}
void Init(){
m.clear();
m['A'] = ;
m['C'] = ;
m['G'] = ;
m['T'] = ;
memset(g,,sizeof(g));
for(int i = ;i <= ;i++){
g[i][i] = ;
}
g[][] = g[][] = -;
g[][] = g[][] = -;
g[][] = g[][] = -;
g[][] = g[][] = -;
g[][] = g[][] = g[][] = g[][] = -;
g[][] = g[][] = g[][] = g[][] = g[][] = g[][] = -;
g[][] = g[][] = -;
}

UVALive 2324 Human Gene Functions(动态规划)的更多相关文章

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

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

  2. POJ1080 Human Gene Functions 动态规划 LCS的变形

    题意读了半年,唉,给你两串字符,然后长度不同,你能够用'-'把它们补成同样长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分 跟LCS非常像的DP数组 dp[i][j]表示第一个字符串取第i个 ...

  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——————【最长公共子序列变型题】

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

  7. 【POJ 1080】 Human Gene Functions

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

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

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

  9. 杭电20题 Human Gene Functions

    Problem Description It is well known that a human gene can be considered as a sequence, consisting o ...

随机推荐

  1. UltraEdit 中的常用正则表达式

    正则表达式 (UltraEdit Syntax):   % 匹配行首 - 表明要搜索的字符串一定在行首.   $ 匹配行尾 - 表明要搜索的字符串一定在行尾   ? 匹配除换行符外的任一单个字符.   ...

  2. debian root 可以远程登陆

    vim /etc/ssh/sshd_config FROM: PermitRootLogin without-password TO: PermitRootLogin yes

  3. MvcPager

    站点网址:http://www.webdiyer.com/mvcpager/ 控制台命令:Install-Package Webdiyer.MvcPager

  4. weblogic一些基本概念

    <收藏过来的----------http://www.cnblogs.com/cocowool/archive/2012/04/01/2428861.html> WebLogic中的一些基 ...

  5. 怎么取消ie浏览器body与html的间隙

    在css文件第一行定义全局样式,可以消除html标签默认间隙*{margin:0;padding:0;}

  6. 十三、oracle 数据字典和动态性能视图

    一.概念数据字典是oracle数据库中最重要的组成部分,它提供了数据库的一些系统信息.动态性能视图记载了例程启动后的相关信息. 二.数据字典1).数据字典记录了数据库的系统信息,它是只读表和视图的集合 ...

  7. memcached + php 扩展 for ubuntu

    1.安装memcached apt-get install memcached 2.安装php memcached 扩展 apt-get install php5-memcache 3.启动memca ...

  8. python常用函数年初大总结

    1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(obj) 得到o ...

  9. HDU1379:DNA Sorting

    Problem Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries ...

  10. 如何在spring中获取request对象

    1.通过注解获取(很简单,推荐): public class Hello {@Autowired  HttpServletRequest request; //这里可以获取到request} 2.在w ...