关闭

DP--ZOJ - 1027

标签: 算法dp
332人阅读 评论(0) 收藏 举报
分类:

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 functions, because these can be used to diagnose human diseases and to design new drugs for them. 

A human gene can be identified through a series of time-consuming biological experiments, often with the help of computer programs. Once a sequence of a gene is obtained, the next job is to determine its function. One of the methods for biologists to use in determining the function of a new gene sequence that they have just identified is to search a database with the new gene as a query. The database to be searched stores many gene sequences and their functions �C many researchers have been submitting their genes and functions to the database and the database is freely accessible through the Internet. 

A database search will return a list of gene sequences from the database that are similar to the query gene. Biologists assume that sequence similarity often implies functional similarity. So, the function of the new gene might be one of the functions that the genes from the list have. To exactly determine which one is the right one another series of biological experiments will be needed. 

Your job is to make a program that compares two genes and determines their similarity as explained below. Your program may be used as a part of the database search if you can provide an efficient one. 

Given two genes AGTGATG and GTTAG, how similar are they? One of the methods to measure the similarity of two genes is called alignment. In an alignment, spaces are inserted, if necessary, in appropriate positions of the genes to make them equally long and score the resulting genes according to a scoring matrix. 

For example, one space is inserted into AGTGATG to result in AGTGAT-G, and three spaces are inserted into GTTAG to result in �CGT--TAG. A space is denoted by a minus sign (-). The two genes are now of equal length. These two strings are aligned: 

AGTGAT-G 
-GT--TAG 

In this alignment, there are four matches, namely, G in the second position, T in the third, T in the sixth, and G in the eighth. Each pair of aligned characters is assigned a score according to the following scoring matrix. 

* denotes that a space-space match is not allowed. The score of the alignment above is (-3)+5+5+(-2)+(-3)+5+(-3)+5=9. 

Of course, many other alignments are possible. One is shown below (a different number of spaces are inserted into different positions): 

AGTGATG 
-GTTA-G 

This alignment gives a score of (-3)+5+5+(-2)+5+(-1) +5=14. So, this one is better than the previous one. As a matter of fact, this one is optimal since no other alignment can have a higher score. So, it is said that the similarity of the two genes is 14. 


Input

The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case consists of two lines: each line contains an integer, the length of a gene, followed by a gene sequence. The length of each gene sequence is at least one and does not exceed 100. 


Output 

The output should print the similarity of each test case, one per line. 


Sample Input 


7 AGTGATG 
5 GTTAG 
7 AGCTATT 
9 AGCTTTAAA


Output for the Sample Input

14 
21


本题类似于最长公共子序列(LCS)问题。


使用gene数组记录动态规划过程中产生的中间结果:
int gene[MaxN][MaxN];   // gene[i][j]表示基因子串str1[0…i-1]和str2[0…j-1]的分值:
str1取第i-1个字母,str2取'-':
m1 = gene[i-1][j] + score[map[str1[i-1]]][4];

str1取'-',str2取第j-1个字母:
m2 = gene[i][j-1] + score[4][map[str2[j-1]]];

str1取第i-1个字母,str2取第j-1个字母:
m3 = gene[i-1][j-1] + score[map[str1[i-1]]][map[str2[j-1]]];

则gene[i][j] = max(m1, m2, m3)。 // 最终结果是gene[first][second]


边界条件就是i或j为0的情况,分3种情况:
(1) 当i=0, j=0时,没有任何基因字母就没有分值,gene[0][0] = 0;
(2) 当i=0时,即为gene [0,1…second] :
for(i = 1 ; i <= second;i++) 
gene[0][i] = gene[0][i-1]+score[4][map[str2[i-1]]]; 
(3) 当j=0时,即为gene [1…first, 0] :
for(i = 1 ; i <= first;i++) 
gene[i][0] = gene[i-1][0]+score[map[str1[i-1]]][4]; 


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;
const int 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 }
						};
char str1[maxn], str2[maxn];
int gene[maxn][maxn], Map[128];

inline int max3(int a, int b, int c)
{
	return max(max(a, b), c);
}

int main()
{
	int T, len1, len2;
	Map['A'] = 0; Map['C'] = 1; Map['G'] = 2; Map['T'] = 3; Map['-'] = 4;
	scanf("%d", &T);
	while(T--) {
        scanf("%d%s%d%s", &len1, str1, &len2, str2);
        gene[0][0] = 0;
        for(int i=1; i<=len2; i++)
			gene[0][i] = gene[0][i-1] + score[4][Map[str2[i-1]]];
		for(int i=1; i<=len1; i++)
			gene[i][0] = gene[i-1][0] + score[Map[str1[i-1]]][4];
        for(int i=1; i<=len1; i++)
			for(int j=1; j<=len2; j++) {
				gene[i][j] = max3(gene[i-1][j] + score[Map[str1[i-1]]][4],
								gene[i][j-1] + score[4][Map[str2[j-1]]],
								gene[i-1][j-1] + score[Map[str1[i-1]]][Map[str2[j-1]]]);
			}
        printf("%d\n", gene[len1][len2]);
	}
    return 0;
}


1
0
查看评论

zoj 1027 - Human Gene Functions

题目:计算两个DNA串的,最大权值公共子序列。
  • mobius_strip
  • mobius_strip
  • 2014-09-27 19:17
  • 376

zoj1027

解析思路: 状态转移方程b[i][j]=max(b[i-1][j-1]+t[i][j],b[i][j-1]+t[4][j],b[i-1][j]+t[i][4]);i表示第一个字符串位子,j表示第二个字符串的位子;b[i][j]表示1到i,1到j位子的值 #include #include #...
  • mdd18703649811
  • mdd18703649811
  • 2012-03-14 22:06
  • 105

zoj1027解题指南

  • 2013-08-20 15:50
  • 773B
  • 下载

zoj 1027 Human Gene Functions

题目链接http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1027
  • u013903504
  • u013903504
  • 2014-08-05 12:53
  • 351

PAT(乙级)1027. 打印沙漏

本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。 ...
  • u012792707
  • u012792707
  • 2016-06-29 21:26
  • 202

浙大PAT 1027题 1027. Colors in Mars

水题,代码写的有点戳 #include int main(){ int r,g,b,cnt; char rst[6]={'0','0','0','0','0','0'}; scanf(&quo...
  • Sup_Heaven
  • Sup_Heaven
  • 2012-12-29 21:49
  • 884

LightOJ-1027

题意: 有n个门, 其中N1个门可以ji
  • u013534690
  • u013534690
  • 2014-04-15 21:46
  • 786

51Nod1027 大数乘法

给出2个大整数A,B,计算A*B的结果。 Input 第1行:大数A 第2行:大数B (A,B的长度 = 0) Output 输出A * B Input示例 123456 234567 Output示例 28958703552 大数乘法,模拟乘法的步骤就...
  • zs120197
  • zs120197
  • 2016-11-30 19:14
  • 175

PAT(甲级)1027

1027. Colors in Mars (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
  • Leonardo1897
  • Leonardo1897
  • 2015-09-24 19:56
  • 209

PAT甲级1027. Colors in Mars (20)

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, wher...
  • liaotl10
  • liaotl10
  • 2017-02-27 23:31
  • 106
    个人资料
    • 访问:557653次
    • 积分:10861
    • 等级:
    • 排名:第1788名
    • 原创:517篇
    • 转载:3篇
    • 译文:0篇
    • 评论:45条