hdu1080 Human Gene Functions(LCS的变型)

来源:互联网 发布:卡盟网站官网程序源码 编辑:程序博客网 时间:2024/04/28 17:07

Human Gene Functions

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2087    Accepted Submission(s): 1178

 

 

Problem Description

It is well known that a human gene can beconsidered as a sequence, consisting of four nucleotides, which are simplydenoted by four letters, A, C, G, and T. Biologists have been interested inidentifying human genes and determining their functions, because these can beused to diagnose human diseases and to design new drugs for them.

 

A human gene can be identified through aseries of time-consuming biological experiments, often with the help ofcomputer programs. Once a sequence of a gene is obtained, the next job is todetermine its function. One of the methods for biologists to use in determiningthe function of a new gene sequence that they have just identified is to searcha database with the new gene as a query. The database to be searched storesmany gene sequences and their functions – many researchers have been submittingtheir genes and functions to the database and the database is freely accessiblethrough the Internet.

 

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

 

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

 

Given two genes AGTGATG and GTTAG, howsimilar are they? One of the methods to measure the similarity of two genes iscalled alignment. In an alignment, spaces are inserted, if necessary, inappropriate positions of the genes to make them equally long and score theresulting genes according to a scoring matrix.

 

For example, one space is inserted intoAGTGATG to result in AGTGAT-G, and three spaces are inserted into GTTAG toresult in –GT--TAG. A space is denoted by a minus sign (-). The two genes arenow 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 theeighth. Each pair of aligned characters is assigned a score according to thefollowing scoring matrix.

 

 

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

 

Of course, many other alignments arepossible. One is shown below (a different number of spaces are inserted intodifferent 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 amatter of fact, this one is optimal since no other alignment can have a higherscore. So, it is said that the similarity of the two genes is 14.

 

 

Input

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

 

 

Output

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

 

 

Sample Input

2

7 AGTGATG

5 GTTAG

7 AGCTATT

9 AGCTTTAAA

 

 

Sample Output

14

21

 

 

Source

Asia 2001, Taejon (South Korea)

题解:

这道题是LCS的变形,同样用的是dp。Char a[]保存第一串字符串,char b[]保存第二串,用score保存值,score[i][j]表示到b[i]a[j]这个地方两串字符串的相似程度。Score[0][0]为0,score[i][0]表示到从a[0]-a[i]所有字符的配对都是‘-’,同样score[0][j]表示从b[0]-b[j]所有值都对值‘-’。而score[i][j]有三种可能。

1.      b[i]和a[j]配对,score[i][j] = score[i-1][j-1]+rec[index[b[i]]][index[a[j]]];

2.      b[i]和’-’配对,score[i][j] = score[i-1][j] + rec[index[b[i]]][index[‘-’]];认定a[j]已经和其他数配对了。

3.      a[j]和’-’配对,score[i][j] = score[i][j-1]+rec[index[a[j]]][index[‘-’]];即认定b[i]已经和其他的数配对了。

三中方案中取最大的值放入score[i][j]中。

最后score[blen][alen]的值就是两个字符串的最大相似程度了。

源代码:

#include <iostream>

#include <map>

#include <stdio.h>

using namespace std;

 

map<char,int>index;

char a[105],b[105];

int score[105][105];

int rec[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,-10000}};

 

int main()

{

   index['A']= 0;index['C'] = 1;index['G'] = 2;

   index['T']= 3;index['-'] = 4;

   intt;

   cin>>t;

   for(int x= 0;x <t;x++)

   {

     intalen,blen;

     cin >> alen;

     a[0]='0';b[0]='0';

     getchar();

     for(int i = 1;i <= alen;i++)

        scanf("%c",&a[i]);

     cin >> blen;

     getchar();

     for(int i = 1;i <= blen;i++)

        scanf("%c",&b[i]);

     score[0][0] = 0;

     for(int i = 1;i <= alen;i++)

        score[0][i] = score[0][i-1] +rec[index[a[i]]][index['-']];

     for(int i = 1;i <= blen;i++)

        score[i][0] = score[i-1][0] +rec[index[b[i]]][index['-']];

 

     intmax = 0;

     for(int i = 1;i <= blen;i++)

        for(int j = 1;j <= alen;j++)

        {

          max = score[i-1][j-1] +rec[index[a[j]]][index[b[i]]];

          inttemp = score[i-1][j] + rec[index['-']][index[b[i]]];

          if(max< temp)

             max = temp;

          temp =score[i][j-1]+rec[index['-']][index[a[j]]];

          if(max< temp)

             max = temp;

          score[i][j] = max;

        }

    

     printf("%d\n",score[blen][alen]);

   }

}

 

0 0
原创粉丝点击