Zoj 1027 Human Gene Functions

Source: Internet
Author: User

Human Gene Functions Time limit: 2 Seconds Memory Limit: 65536 KB

It's well known a human gene can considered as a sequence, consisting of four nucleotides, which was simply Denot Ed by four letters, A, C, G, and T. Biologists has been interested in identifying human genes and determining their funct Ions, because these can be used to diagnose human diseases and to design new drugs for them.

A human gene can 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 was to determine its function. One of the methods for biologists to use in determining the function of a new gene sequence that they has just identified IS-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 has been submitting their genes and functions to the database and the database are freely accessible th Rough the Internet.

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

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

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

For example, one space was inserted into AGTGATG to the result in agtgat-g, and three spaces was inserted into gttag to result In? Cgt--tag. A space is denoted by a minus sign (-). The genes is now of equal length. These strings is aligned:

Agtgat-g
-gt--tag

In this alignment, there is 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 are assigned a score according to the following scoring matrix.

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

Of course, many other alignments is possible. One is shown below (a different number of spaces be 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 was optimal since no other alignment can has a higher score. So, it's said that the similarity of the the 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 Lines:each line contains a integer, the length of a gene, followed by a gene sequence. The length of each gene sequence are at least one and does not exceed 100.


Output

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


Sample Input

2
7 AGTGATG
5 Gttag
7 Agctatt
9 AGCTTTAAA


Output for the Sample Input

14
21st

Dynamic planning can be easily solved, and the recurrence relationship is as follows:

S (s1, s2) = Max{s (S1-1, s2-1) + similarity (S1.back (), S2.back ()), \

S (S1, s2-1) + similarity ('-', S2.back ()),

S (s1-1, S2) + similarity (S1.back (), '-')};

1#include <iostream>2#include <cmath>3#include <cstdio>4#include <vector>5#include <list>6#include <string>7#include <cstring>8#include <cstdio>9#include <algorithm>Ten#include <Set> One  A using namespacestd; -  - intIndexCharc) the { -     CharCh[] = {'A','C','G','T','-' }; -      for(inti =0; I <5; i++) -     { +         if(Ch[i] = =c) -             returni; +     } A  at     return-1; - } -  - intSimilarity (CharC1,CharC2) - { -     intscore[5][5] = { {5, -1, -2, -1, -3 },     in{ -1,5, -3, -2, -4 },     -{ -2, -3,5, -2, -2 },     to{ -1, -2, -2,5, -1 },     +{ -3, -4, -2, -1,0 } }; -  the     returnScore[index (C1)][index (C2)]; * } $ Panax Notoginseng intMaxsim (stringS1,stringS2) - { the     intdp[ the][ the]; +      Adp[0][0] =0; theS1 ='-'+S1; +S2 ='-'+S2; -      for(inti =1; I < s1.size (); i++) $dp[0][i] = dp[0][i-1] + similarity ('-', S1[i]); $      for(inti =1; I < s2.size (); i++) -dp[i][0] = dp[i-1][0] + similarity ('-', S2[i]); -  the      for(inti =1; I < s2.size (); i++) -     {Wuyi          for(intj =1; J < S1.size (); J + +) the         { -             intscore[3] = { WuDp[i-1][j-1] +similarity (S1[j], s2[i]), -Dp[i-1][J] + similarity (S2[i],'-'), AboutDp[i][j-1] + similarity ('-', S1[j]) $             }; -  -DP[I][J] = *max_element (score, score +3); -         } A     } +  the     returnDp[s2.size ()-1][s1.size ()-1]; - } $  the intMain () the { the     intN; theCIN >>N; -  in      while(n--) the     { the         intlen1, Len2; About         strings1, S2; theCin >> len1 >> S1 >> len2 >>S2; the      thecout << Maxsim (S1, S2) <<Endl; +     } -}

Zoj 1027 Human Gene Functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.