题目链接

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

就是 最长公共子序列的 的思想: 首先对于 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. 深入理解Message, MessageQueue, Handler和Looper

    做过Android的都知道Message, MessageQueue, Handler和Looper,但知道不代表你理解它们.有时觉得用得很顺手,但Android怎么实现又说不上来,总觉得似懂非懂.不 ...

  2. python作为一种胶水和c/c++

    如果需要用 Python 调用 C/C++ 编写的第三方库,只需要一个脚本语言来粘合它们.这个时候,用 Python ctypes 可以很方便地实现调用. StackOverflow 上的 Calli ...

  3. MAC OS上Nginx安装

    admin@admindeMac:local]$ brew install nginx ==> Installing dependencies for nginx: pcre, openssl ...

  4. Project Serve 2013部署方法

    在线版Project2013部署手册 服务器环境要求 系统:windows server 2008r2.windows server2012x64 Sharepoint 2013 内存至少16GB,最 ...

  5. python基础_字典_列表_元组考试_day4

    1.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li=['alex','eric','rain'] li=['alex','eric','rain'] v="_".jo ...

  6. linux中级-JAVA企业级应用TOMCAT实战

    1. Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共 ...

  7. JNI系列——C文件中使用logcat

    1.在Android.mk文件中添加:LOCAL_LDLIBS += -llog 注:加载的这个库在NDK对应平台目录下的lib目录中. 2.在C文件中添加如下内容: #include <and ...

  8. Java--剑指offer(3)

    11.输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. a)使用Integer.toBinaryString(n);来计算得出二进制的字符串,然后使用for循环截取字符串是否为1 pu ...

  9. selector 和 shape结合使用

    <?xml version="1.0" encoding="utf-8"?><selector xmlns:android="htt ...

  10. 不停止MySQL服务的情况下修改root的密码

    首先我们得知道一个MySQL普通用户的密码 这里我来记录一下我的操作过程 这里我刚刚到一家公司上面装的是cacti,但是之前的运维不记得MySQL的root密码了 但是他知道cacti的密码, 用户: ...