题目链接

题目:给出两个串,每匹配一种有一种权值,求权值最大的匹配串

就是 最长公共子序列的 的思想: 首先对于 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变形)的更多相关文章

  1. poj1080--Human Gene Functions(dp:LCS变形)

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

  2. POJ 1080:Human Gene Functions LCS经典DP

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

  3. POJ1080 Human Gene Functions(LCS)

    题目链接. 分析: 和 LCS 差不多. #include <iostream> #include <cstdio> #include <cstdlib> #inc ...

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

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

  5. 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 ...

  6. 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 ...

  7. POJ 1080( LCS变形)

    题目链接: http://poj.org/problem?id=1080 Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K ...

  8. hdu 1080(LCS变形)

    Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  9. POJ1080(LCS变形)

    Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. JavaScript学习笔记- 省市级联效果

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  2. 基于jquery实现的上传图片及图片预览效果代码

    <!DOCTYPE html> <html> <head> <title>HTML5上传图片预览</title> <meta http ...

  3. 自定义CoordinatorLayout Behavior 隐藏Footer View

    在用新的控件中,我们可以用Toolbar与CoordinatorLayout实现 向上滚动隐藏的效果,可是官方并没有找到向上隐藏底部导航的功能,有一些第三方的框架实现了. 在Android M,Coo ...

  4. #查看Linux的版本信息

    查看Ubuntu的版本信息 cat /etc/issue lsb_release -a 查看Centos的版本信息 cat /etc/centos-release rpm -q centos-rele ...

  5. iOS开发--二维码的生成

    一.需要包含头文件 #import <CoreImage/CoreImage.h> 二.示例代码 -- 以下生成的二维码不够清晰 如图: - (void)touchesBegan:(NSS ...

  6. 【线性规划与网络流 24题】已完成(3道题因为某些奇怪的原因被抛弃了QAQ)

    写在前面:SDOI2016 Round1滚粗后蒟蒻开始做网络流来自我拯救(2016-04-11再过几天就要考先修课,现在做网络流24题貌似没什么用←退役节奏) 做的题目将附上日期,见证我龟速刷题. 1 ...

  7. bzoj1503

    treap改了好长时间,erase写错了... #include<iostream> #include<cstdio> #include<cstdlib> usin ...

  8. lucene-查询query->QueryParser

    对于搜索引擎(比如Google和百度)来讲,很多情况下只需要用户在输入框内输入所需查询的内容,然后再单击“搜索”就可以了,其余的事情全部交给搜索引擎去处理,最后搜索引擎会把检索到的结果显示出来.那么搜 ...

  9. sql-将查询字段拼接起来

    sql语句 select (a + b) as c mysql不起作用

  10. Java中Comparable和Comparator区别小结

    一.Comparable简介 Comparable是排序接口.若一个类实现了Comparable接口,就意味着该类支持排序.实现了Comparable接口的类的对象的列表或数组可以通过Collecti ...