您的位置:首页 > 其它

hdu 1080 Human Gene Functions

2016-08-21 14:47 323 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1080

题目描述:

[align=left]Problem Description[/align]
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 – 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 –GT--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.

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
The output should print the similarity of each test case, one per line.

 

[align=left]Sample Input[/align]

2
7 AGTGATG
5 GTTAG
7 AGCTATT
9 AGCTTTAAA

 

[align=left]Sample Output[/align]

14
21

题目大意:

给你两个字符串,可以在字符串任意位置添加空格,不同字母间相对应的价值如上图,问,所能达到的最大价值是多少,注意,空格和空格不能匹配

题目分析:

二维数组dp[i][j]代表到达s1串第i个字符,s2串第j个字符,价值的最大者.情况一共只有三种,要么s1串第i个和s2串第j个匹配,要么s1串第i-1个与s2串第j个匹配然后s1串第i个与空格匹配,要么s1串第i个和s2串第j-1个匹配然后s2串第j个和空格匹配

Ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;

int dp[110][110],map[110][110];
int l1,l2;
char s1[110],s2[110];
int i,j,t;

void WW()//打出价值图表
{
map['A']['A']=map['C']['C']=map['G']['G']=map['T']['T']=5;
map['A']['C']=map['C']['A']=map['A']['T']=map['T']['A']=map['T'][' ']=map[' ']['T']=-1;
map['A']['G']=map['C']['T']=map['G']['A']=map['G']['T']=map['G'][' ']=map['T']['C']=map['T']['G']=map[' ']['G']=-2;
map['A'][' ']=map['C']['G']=map['G']['C']=map[' ']['A']=-3;
map['C'][' ']=map[' ']['C']=-4;

}

int main()
{
WW();
cin>>t;
while(t--)
{
scanf("%d %s",&l1,s1+1);
scanf("%d %s",&l2,s2+1);
dp[0][0]=0;//初始化,没有任何一个匹配项,那么价值自然是0
for(i=1;i<=l1;i++)//初始化第1列,即视s2为空,初始化
dp[i][0]=dp[i-1][0]+map[s1[i]][' '];
for(i=1;i<=l2;i++)//初始化第1行,即视s1为空,初始化
dp[0][i]=dp[0][i-1]+map[' '][s2[i]];
//dp[i][j]代表到达s1串第i个字符,s2串第j个字符,价值的最大者
for(i=1;i<=l1;i++)
{
for(j=1;j<=l2;j++)
{
dp[i][j]=max(dp[i-1][j]+map[s1[i]][' '],dp[i][j-1]+map[' '][s2[j]]);//取s1串第i个与空格匹配,s2串第j个与空格匹配的较大者
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+map[s1[i]][s2[j]]);//取s1串第i个跟s2串第j个相匹配与前面比较的较大者的较大者
}
}
cout<<dp[l1][l2]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: