zoj1027 Human Gene Functions

一道动态规划,两个串进行匹配,不同字母匹配的值不一样,也可以和空格匹配(空格不能与空格匹配),求最大的匹配值。

数据很弱,每个串都在100以内。

定义dp[i][j]为第一个串前i个数和第二个串前j个数已匹配的匹配值

有三种情况:1.第i个和第j个匹配

                      2.第i个和‘-’匹配

                      3.第j个和‘-’匹配

注意合理初始化

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
using namespace std;
int g[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}};
char str1[105];
char str2[105];
map<char,int> m;
int cas;
int dp[105][105];//前i个数和前j个数已匹配的情况
int main()
{
  // freopen("input.txt","r",stdin);
   int len1,len2;
   scanf("%d",&cas);
   m[‘A‘]=0;
   m[‘C‘]=1;
   m[‘G‘]=2;
   m[‘T‘]=3;
   m[‘-‘]=4;
   while(cas--)
   {
     scanf("%d%s",&len1,str1);
     scanf("%d%s",&len2,str2);
     dp[0][0]=0;
     for(int i=1;i<=len1;i++)
        dp[i][0]=dp[i-1][0]+g[m[str1[i-1]]][4];
     for(int i=1;i<=len2;i++)
        dp[0][i]=dp[0][i-1]+g[4][m[str2[i-1]]];
     for(int i=1;i<=len1;i++)
        for(int j=1;j<=len2;j++)
     {
         int t1=dp[i-1][j-1]+g[m[str1[i-1]]][m[str2[j-1]]];
         int t2=dp[i-1][j]+g[m[str1[i-1]]][4];
         int t3=dp[i][j-1]+g[4][m[str2[j-1]]];
         dp[i][j]=max(t1,max(t2,t3));
     }
     printf("%d\n",dp[len1][len2]);
   }
}

 

时间: 2024-09-28 06:35:31

zoj1027 Human Gene Functions的相关文章

POJ 1080 Human Gene Functions(LCS)

Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their

POJ1080 Human Gene Functions 动态规划 LCS的变形

题意读了半年,唉,给你两串字符,然后长度不同,你可以用'-'把它们补成相同长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分 跟LCS很像的DP数组 dp[i][j]表示第一个字符串取第i个元素第二个字符串取第三个元素,然后再预处理一个得分表加上即可 得分表: score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5; score['A']['C'] = score['C']['A'] = -1;

POJ 1080 Human Gene Functions(DP)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18007   Accepted: 10012 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four

zoj 1027 Human Gene Functions

Human Gene Functions Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been

poj 1080 ——Human Gene Functions——————【最长公共子序列变型题】

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17805   Accepted: 9917 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four

P 1080 Human Gene Functions

大概作了一周,终于A了 类似于求最长公共子序列,稍有变形 当前序列 ch1 中字符为 a,序列 ch2 中字符为 b 则有 3 种配对方式: 1. a 与 b 2. a 与 - 3. - 与 b 动态转移方程: dp[i][j] = max(dp[i - 1][j - 1] + g(ch1[i],ch2[j]) , dp[i - 1][j] + g(ch1[i],'-') , dp[i][j-1] + g('-',ch2[j])) 代码如下: #include<stdio.h> #includ

poj 1080 Human Gene Functions(lcs,较难)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19573   Accepted: 10919 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four

POJ - 1080 - Human Gene Functions (LCS的变形)

题目传送:Human Gene Functions 思路:LCS的变形,定义状态dp[ i ][ j ]为取字符串s前i个字符字符串t前j个字符所获得的最大值,则可以得到状态转移方程为: dp[ i ][ j ] = max(dp[ i ][ j - 1] + f[ ' - ' ][ t[ j ] ], dp[ i - 1 ][ j ] + f[ s [ i ] ][ ' - ' ], dp[i - 1][ j - 1] + f[ s [ i ] ][ t [ j ] ]); AC代码: #in

HDU 1080 Human Gene Functions

最长公共子序列的变形 题目大意:给出两个基因序列,求这两个序列的最大相似度. 题目中的表格给出了两两脱氧核苷酸的相似度. 状态转移方程为: dp[i][j] = max(dp[i-1][j]+Similarity(s1[i], '-'),                     dp[i][j-1]+Similarity(s2[j], '-'),                     dp[i-1][j-1]+Similarity(s1[i], s2[j])); 注意边界的初始化. 1 //#