HDU 1080 Human Gene Functions(带权值的最长公共子序列)
2016-03-25 16:31
429 查看
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1080
思路:最长公共子序列
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
map<char,int> map1;
map1['A'] = 0;
map1['C'] = 1;
map1['G'] = 2;
map1['T'] = 3;
map1['-'] = 4;
int t,first,second,i,j,m1,m2,m3;
char gene1[105],gene2[105];
int dp[105][105],score[5][5] = {{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,0}};
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d%s",&first,gene1);
scanf("%d%s",&second,gene2);
for(i=1; i<=second; i++)//第一个已经用完
dp[0][i] = dp[0][i-1] + score[4][map1[gene2[i-1]]];
for(i=1; i<=first; i++)//第二个已经用完
dp[i][0] = dp[i-1][0] + score[map1[gene1[i-1]]][4];
for(i=1; i<=first; i++)
{
for(j=1; j<=second; j++)
{
m1 = dp[i-1][j] + score[map1[gene1[i-1]]][4];//第二个字符串取-
m2 = dp[i][j-1] + score[4][map1[gene2[j-1]]];//第一个字符串取-
m3 = dp[i-1][j-1] + score[map1[gene1[i-1]]][map1[gene2[j-1]]];//都不取
dp[i][j] = max(m1,max(m2,m3));
}
}
printf("%d\n",dp[first][second]);
}
return 0;
}
思路:最长公共子序列
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
map<char,int> map1;
map1['A'] = 0;
map1['C'] = 1;
map1['G'] = 2;
map1['T'] = 3;
map1['-'] = 4;
int t,first,second,i,j,m1,m2,m3;
char gene1[105],gene2[105];
int dp[105][105],score[5][5] = {{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,0}};
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%d%s",&first,gene1);
scanf("%d%s",&second,gene2);
for(i=1; i<=second; i++)//第一个已经用完
dp[0][i] = dp[0][i-1] + score[4][map1[gene2[i-1]]];
for(i=1; i<=first; i++)//第二个已经用完
dp[i][0] = dp[i-1][0] + score[map1[gene1[i-1]]][4];
for(i=1; i<=first; i++)
{
for(j=1; j<=second; j++)
{
m1 = dp[i-1][j] + score[map1[gene1[i-1]]][4];//第二个字符串取-
m2 = dp[i][j-1] + score[4][map1[gene2[j-1]]];//第一个字符串取-
m3 = dp[i-1][j-1] + score[map1[gene1[i-1]]][map1[gene2[j-1]]];//都不取
dp[i][j] = max(m1,max(m2,m3));
}
}
printf("%d\n",dp[first][second]);
}
return 0;
}
相关文章推荐
- UDP协议的两个主要方法sendto和recvfrom详解
- error: No resource identifier found for attribute 'cardCornerRadius' in package XXXXXX
- 朴素模式匹配算法java实现
- leetcode-12 Integer to Roman
- 重载函数编译后的新名字
- Qt的4个图像类QImage/QPixmap/QBitmap/QPicture
- 《MySQL学习笔记》:查询
- JVM的内存划分
- ubuntu 14 x86_64 nginx rtmp ffmpeg 在线直播和点播
- UIViewController - SenDylan
- PHP 函数
- [Cordova] 手机网页里的1px
- 博客搬家
- Java字符串全角转半角
- leetcode-10 Regular Expression Matching
- Struts.xml 常用常量配置
- 史上最全面最精华的Linux内存相关资料
- 垃圾车定律
- spring 动态定时器配置
- 两个input之间有空隙,处理方法