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 ...
随机推荐
- 深入学习JavaScript(二)
函数表达式和函数声明 函数声明 function 函数名(参数){函数体} 函数表达式 function 函数名(可选)(参数){函数体} 示例: function foo(){} // 声明,因为它 ...
- mutex与semaphore的区别
网摘1:Mutex 的发音是 /mjuteks/ ,其含义为互斥(体),这个词是Mutual Exclude的缩写.Mutex在计算机中是互斥也就是排他持有的一种方式,和信号量-Semaphore有可 ...
- 【JavaEE企业应用实战学习记录】MyGetAttributeListener
package sanglp.servlet; import javax.servlet.ServletContext; import javax.servlet.ServletContextAttr ...
- android 布局之scrollview
今天在布局页面的时候后犯了难,我要显示的内容一个页面展示不完,怎么办呢? 于是随便找了个app点开一看,哎呀原来还能翻动啊!这是啥布局呢?原来是ScrollView 官方api相关的内容全是英文,这可 ...
- git 格式化输出版本信息
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&l ...
- 【POJ 1228】Grandpa's Estate 凸包
找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include ...
- Shiro 学习笔记(二)——shiro身份验证
身份验证: 在应用中证明他就是他本人.一般上用身份证.用户/密码 来证明. 在shiro中,用户需要提供principals (身份)和credentials(证明)给shiro,从而应用能验证用户身 ...
- Js-动态控制table的tr、td增加及删除的具体实现
<table id='wifi_clients_table' style="color:#0099CC;font-size:12px;" class="table ...
- iis 发布静态 html 代码
参考地址: http://jingyan.baidu.com/article/c843ea0b7d0b7177921e4a68.html 操作步骤:1.在当前发布网站中建立虚拟目录 2.点击当前网站 ...
- PHP递归生成树形数组
数据表结构 id name pid ){ foreach($data as $row){ if($row['pid']==$p_id){ $tmp = $this->tree( ...