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

  思路:这个可以说是编辑距离的一个变形,编辑距离最终状态时要两个字符串完全一致,这个就是要求长度一样,而且这个只允许插入“—”这一个字符。模仿编辑距离定义状态,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. Arch声卡配置

    ALSA Utilities Install the alsa-utils package. This contains (among other utilities) the alsamixer a ...

  2. IP子网掩码格式转换

    def exchange_maskint(mask_int): bin_arr = [' for i in range(32)] for i in range(mask_int): bin_arr[i ...

  3. 安装、配置、启动FTP、SSH或NFS服务

    (1)准备使用软件维护工具apt-get. Ubuntu7.10中没有安装FTP.SSH.NFS服务器软件,它提供了一个很方便的安装.升级.维护软件的工具apt-get.apt-get从光盘.网络上下 ...

  4. Android测试日志文件抓取与分析

    1.log文件分类简介 实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb shel ...

  5. html 框架属性

    <html>    <head>        <title></title>    </head>    <frameset col ...

  6. Servlet基础知识

    基本知识一.Web结构1.两种应用程序 ①桌面应用程序:QQ.CS.MyEclipse.Office.DW.360.浏览器等必须下载.安装.桌面快捷方式.注册表信息.操作系统后台服务.占用操作系统端口 ...

  7. linux下shell命令trap

    某些时候,在执行shell脚本(.sh)时,我们并不希望被打断.这时我们要用到trap命令. 例如: 在shell脚本中,忽略“终止”信号 trap  ' '   TERM

  8. ice使用过程遇到的问题

    1 设置代理超时时间ice_timeout ICE的每个连接都有两个超时时间:ice_timeout.ice_connectiontimeout,分别对应消息的超时时间和连接建立    的超时时间,可 ...

  9. Entity Framework技巧系列之九 - Tip 35 - 36

    提示35. 怎样实现OfTypeOnly<TEntity>()这样的写法 如果你编写这样LINQ to Entities查询: 1 var results = from c in ctx. ...

  10. SQL Server 存储过程进行分页查询

    CREATE PROCEDURE prcPageResult -- 获得某一页的数据 -- @currPage INT = 1 , --当前页页码 (即Top currPage) @showColum ...