POJ1080Human Gene Functions(LCS变形)
题目:给出两个串,每匹配一种有一种权值,求权值最大的匹配串
就是 最长公共子序列的 的思想: 首先对于 i 和 j 来比较, 一种情况是i和j匹配,此时 dp[i][j] = dp[i - 1][j - 1] + g[ str1[i] ][ str2[j] ],另一种情况是i和j不匹配,那么就有两种情况,一 i 和 j前面的匹配,j与一个空 即 ‘ - ’匹配,dp[i][j] = dp[i ][ j - 1] + g[ ' - ' ][ str2[j] ] ,二 i 前面的 和 j匹配上,此时 i 和 ‘ - ’匹配,dp[i][j] = dp[i - 1][ j] + g[ str1[i] ][ '-' ],三种情况取最大。
这题就败在了初始化上=_=
第一次只初始化了dp[0][0],后来将 dp[1][0]和dp[0][1]又初始化了,其实要把 dp【0】【i] 和 dp[i][0] 和 dp【0][0] 都要初始化,这也是很明显的=_=
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int Max = ;
int g[][] = { {, -, -, -, - }, { -, , -, -, - }, { -, -, , -, -} , { -, -, -, , -}, {-, -, -, -, INF} };
map<char, int> m; // 为每一个 字母建立一个映射
int dp[Max][Max];
int main()
{
m['A'] = ;
m['C'] = ;
m['G'] = ;
m['T'] = ;
m['-'] = ;
int T;
scanf("%d", &T);
while (T--)
{
int len1, len2;
char str1[Max], str2[Max];
scanf("%d %s", &len1, str1 + );
scanf("%d %s", &len2, str2 + );
dp[][] = ;
for (int i = ; i <= len2; i++)
dp[][i] = dp[][i - ] + g[][ m[str2[i]] ];
for (int i = ; i <= len1; i++)
dp[i][] = dp[i - ][] + g[ m[str1[i]] ][]; //cout << str1 + 1 << endl;
//cout << str2 + 1 << endl;
for (int i = ; i <= len1; i++)
{
for (int j = ; j <= len2; j++)
{
dp[i][j] = dp[i - ][j - ] + g[ m[str1[i]] ][ m[str2[j]] ];
//if (dp[i - 1][j] + g[ m[str1[i]] ][4] INF)
dp[i][j] = max(dp[i][j], dp[i - ][j] + g[ m[str1[i]] ][]); //其实初始化在这里很明显,因为要用dp[i - 1][j】,当 i= 1时,必须知道所有的 dp[0][j]+_+
//if (dp[i][j - 1] + g[4][ m[str2[j]] ] != INF)
dp[i][j] = max(dp[i][j], dp[i][j - ] + g[][ m[str2[j]] ]);
}
}
printf("%d\n", dp[len1][len2]);
}
return ;
}
POJ1080Human Gene Functions(LCS变形)的更多相关文章
- poj1080--Human Gene Functions(dp:LCS变形)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17206 Accepted: ...
- POJ 1080:Human Gene Functions LCS经典DP
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18007 Accepted: ...
- POJ1080 Human Gene Functions(LCS)
题目链接. 分析: 和 LCS 差不多. #include <iostream> #include <cstdio> #include <cstdlib> #inc ...
- poj 1080 Human Gene Functions(lcs,较难)
Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19573 Accepted: ...
- poj 1080 (LCS变形)
Human Gene Functions 题意: LCS: 设dp[i][j]为前i,j的最长公共序列长度: dp[i][j] = dp[i-1][j-1]+1;(a[i] == b[j]) dp[i ...
- 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( LCS变形)
题目链接: http://poj.org/problem?id=1080 Human Gene Functions Time Limit: 1000MS Memory Limit: 10000K ...
- hdu 1080(LCS变形)
Human Gene Functions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- POJ1080(LCS变形)
Human Gene Functions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
随机推荐
- retrofit2中ssl的Trust anchor for certification path not found问题
在retrofit2中使用ssl,刚刚接触,很可能会出现如下错误. java.security.cert.CertPathValidatorException: Trust anchor for ce ...
- sencha xtype清单
xtype Class ----------------- --------------------- actionsheet Ext.ActionSheet audio Ext.Audio butt ...
- (十一)外观模式详解(Service第三者插足,让action与dao分手)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 各位好,LZ今天给各位分享一 ...
- 深入理解计算机系统(4.1)---X86的孪生兄弟,Y86指令体系结构
引言 各位猿友们好,计算机系统系列很久没更新了,实在是抱歉之极.新的一年,为了给计算机系统系列添加一些新的元素,LZ将其更改为书的原名<深入理解计算机系统>.这本书非常厚,而且难度较高,L ...
- Python使用基础
1) 基本概念1.1 常量 Python没有提供常量保留字,需要自行扩展一个常量类来实现常量功能 class _const: class ConstError(TypeError):pass def ...
- js表单提交,面向对象
一.js表单验证之后再提交 1.普通按钮onclick函数调用表单的submit()函数 <input type=button name="submit1" value=&q ...
- redis的主从复制配置
redis的主从复制配置 一. 原理 Redis的主从复制功能非常强大,一个master可以拥有多个slave,而一个slave又可以拥有多个slave,如此下去,形成了强大的多级服务器集群架 ...
- python环境搭建-在Windows上安装python3.5.2
在Windows上安装Python3.5.2 首先,根据你的Windows版本(64位还是32位)从Python的官方网站下载Python 3.5.2对应的64位安装程序或32位安装程序(网速慢的同学 ...
- Android延时执行的几种方法
开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...
- [转] EJB到底是什么,真的那么神秘吗??
原文地址:http://blog.csdn.net/jojo52013145/article/details/5783677 1. 我们不禁要问,什么是"服务集群"?什么是&quo ...