UVALive 2324 Human Gene Functions(动态规划)
题意:求出将两个字符串改成一样长度所能形成最大的相似度。
思路:这个可以说是编辑距离的一个变形,编辑距离最终状态时要两个字符串完全一致,这个就是要求长度一样,而且这个只允许插入“—”这一个字符。模仿编辑距离定义状态,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(动态规划)的更多相关文章
- POJ 1080 Human Gene Functions -- 动态规划(最长公共子序列)
题目地址:http://poj.org/problem?id=1080 Description It is well known that a human gene can be considered ...
- POJ1080 Human Gene Functions 动态规划 LCS的变形
题意读了半年,唉,给你两串字符,然后长度不同,你能够用'-'把它们补成同样长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分 跟LCS非常像的DP数组 dp[i][j]表示第一个字符串取第i个 ...
- poj 1080 Human Gene Functions(lcs,较难)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19573 Accepted: ...
- Human Gene Functions
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18053 Accepted: 1004 ...
- 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 ...
- poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17805 Accepted: ...
- 【POJ 1080】 Human Gene Functions
[POJ 1080] Human Gene Functions 相似于最长公共子序列的做法 dp[i][j]表示 str1[i]相应str2[j]时的最大得分 转移方程为 dp[i][j]=max(d ...
- POJ 1080:Human Gene Functions LCS经典DP
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18007 Accepted: ...
- 杭电20题 Human Gene Functions
Problem Description It is well known that a human gene can be considered as a sequence, consisting o ...
随机推荐
- [kuangbin带你飞]专题四 最短路练习 POJ 2253 Frogger
求第一个点到第二个点的所有通路上最长的边 dijkstra的变形 每次松弛的是每条边通路上的的最长的边 WA了好几次是因为用了%lf 改成%f就过了…… /* ******************** ...
- Openjudge-计算概论(A)-求一元二次方程的根
描述: 利用公式x1 = (-b + sqrt(b*b-4*a*c))/(2*a), x2 = (-b - sqrt(b*b-4*a*c))/(2*a)求一元二次方程ax2 + bx + c =0的根 ...
- Cash Machine
Problem Description A Bank plans to install a machine for cash withdrawal. The machine is able to de ...
- 关于oracle数据库(8)查询2
筛选数据,直接加where条件,并且and,或者or 使用rownum获取前N条数据 select * from 表名 where rownum <= 数字; 如:获取前5条数据 select ...
- CVE-2014-4115漏洞分析(2014.11)
CVE-2014-4115漏洞分析 一.简介 该漏洞是由于Windows的Fastfat.sys组件在处理FAT32格式的硬盘分区存在问题.攻击者利用成功可导致权限提升. 影响的系统包括: Windo ...
- JavaScript(4)——闭包与this对象以及window对象
闭包与this对象以及window对象 这次写的是这三个内容.其实在写之前,会觉得这三个内容很多,但是写了之后会发现,内容确实很多,但是可以写出来的也并不是很多.可能是我总结能力太差.但是这些内容我觉 ...
- bootstrap开始咯
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- javascript 事件响应
1.基本事件 2.点击事件 <html> <head> <script type="text/javascript"> function add ...
- FZU 1502 Letter Deletion
最长公共子序列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- java中static关键字解析
static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...