题目链接:

https://cn.vjudge.net/problem/POJ-1080

题目大意:

给定两组序列,要你求出它们的最大相似度,每个字母与其他字母或自身和空格对应都有一个打分,求在这两个字符串中插入空格,让这两个字符串的匹配分数最大

解题思路:

类似LCS,以dp[i][j]表示s1前i位和s2前j位的最优解。

递推式为:

先不考虑括号

dp[i][j]只由dp[i-1][j-1]递推而来

if(s1[i] == s2[j])dp[i][j] = dp[i - 1][j - 1] + 5

else dp[i][j] = dp[i - 1][j - 1] + Map[id1][id2]此处Map[id1][id2]为s1[i]和s2[j]匹配的分数

考虑括号

dp[i][j]在原来基础上可以由dp[i-1][j]和dp[i][j-1]递推而来

由dp[i-1][j]递推而来的时候是s1[i]和空格匹配

由dp[i][j-1]递推而来的时候是空格和s2[j]匹配

dp[i][j] = max(dp[i][j],dp[i-1][j] + Map[id1][空格], dp[i][j - 1] + Map[空格][id2])

注意:

要初始化dp[0][i]和dp[j][0]的值(不能单纯的置为0,置为0是错误的),dp[0][0]=0

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
#define Mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int INF = 1e9+;
int dp[][];
int Map[][] =
{
,-,-,-,-,
-,,-,-,-,
-,-,,-,-,
-,-,-,,-,
-,-,-,-,,
};
char s1[], s2[];
int main()
{
map<char, int>id;
id['A'] = ;
id['C'] = ;
id['G'] = ;
id['T'] = ;
id[' '] = ;
int T, n, m;
cin >> T;
while(T--)
{
cin >> n >> (s1 + );
cin >> m >> (s2 + );
Mem(dp, );
for(int i = ; i <= n; i++)
{
dp[i][] = dp[i - ][] + Map[id[s1[i]]][id[' ']];
}
for(int i = ; i <= m; i++)
{
dp[][i] = dp[][i - ] + Map[id[' ']][id[s2[i]]];
}
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
if(s1[i] == s2[j])
dp[i][j] = dp[i - ][j - ] + ;
else
dp[i][j] = dp[i - ][j - ] + Map[id[s1[i]]][id[s2[j]]];
//cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
dp[i][j] = max(dp[i][j], dp[i - ][j] + Map[id[s1[i]]][id[' ']]);
dp[i][j] = max(dp[i][j], dp[i][j - ] + Map[id[' ']][id[s2[j]]]);
//cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
}
//
}
cout<<dp[n][m]<<endl;
}
return ;
}

POJ-1080 Human Gene Functions---类似LCS的更多相关文章

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

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

  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 -- 动态规划(最长公共子序列)

    题目地址: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. dp poj 1080 Human Gene Functions

    题目链接: http://poj.org/problem?id=1080 题目大意: 给两个由A.C.T.G四个字符组成的字符串,可以在两串中加入-,使得两串长度相等. 每两个字符匹配时都有个值,求怎 ...

  6. POJ 1080 Human Gene Functions

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

  7. POJ 1080 Human Gene Functions 【dp】

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

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

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

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

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

  10. HDU 1080 Human Gene Functions - 最长公共子序列(变形)

    传送门 题目大意: 将两个字符串对齐(只包含ACGT,可以用'-'占位),按照对齐分数表(参见题目)来计算最后的分数之和,输出最大的和. 例如:AGTGATG 和 GTTAG ,对齐后就是(为了表达对 ...

随机推荐

  1. centos7安装nslookup工具、ntp工具

    2018-12-13 centos7安装nslookup工具 yum install bind-utils -y DNS解析localhost到本机 # .检测 [root@node2 ~]# nsl ...

  2. nodejs的一些学习

    要使用npm的时候,其实是可以直接下载node.js的.参考文档http://www.runoob.com/nodejs/nodejs-npm.html 安装成功之后.判断是否安装成功.是不能直接用n ...

  3. PIE SDK去相关拉伸

    1.算法功能简介 由于高度相关的数据集经常生成十分柔和的彩色图像,因此经常使用 去相关拉伸工具来体消除多光谱数据集中的高度相关性, 从而生成一幅色彩亮丽的彩色合成图像.去相关拉伸需要 3 个输入波段, ...

  4. vue组件(持续更新)

    1.vee-validate :vue的表单验证组件 网友博客介绍:https://www.cnblogs.com/xxwang/p/6104715.html

  5. win10 sshsecureshellclient删除profile保存的信息

    C:\Users\joe\AppData\Roaming\SSH

  6. TOJ 2749 Absent Substrings

    描述 Given a string of symbols, it’s natural to look it over and see what substrings are present. In t ...

  7. hibernate表关系

    1.一对一 用户表可以查分成两个表,一个userInfo.一个userLogin表 实现方式: (1)使用外键:外键+唯一性约束+非空约束 (2)公用主键:公用主键,从表的主键同时也是外键,来源于主表 ...

  8. 通过response向服务器用Io流写入图片

       1.响应头设置字节.     使用response获得字节输出流        ServletOutputStream out = response.getOutputStream();     ...

  9. 【Spring Cloud】与Spring Boot版本匹配关系

    Spring Cloud版本演进情况如下: 版本名称 版本Finchley snapshot版Edgware snapshot版Dalston SR1 当前最新稳定版本Camden SR7 稳定版本B ...

  10. iOS开发之GCD基础

    重新回顾.学习GCD.Block.先贴出一篇不错的讲解GCD基础使用的文章 原文地址:http://blog.csdn.net/aolan1108/article/details/17283415 做 ...