Chinaunix首页 | 论坛 | 博客
  • 博客访问: 474236
  • 博文数量: 117
  • 博客积分: 3195
  • 博客等级: 中校
  • 技术积分: 1156
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-04 01:44
文章分类

全部博文(117)

文章存档

2012年(5)

2011年(5)

2010年(46)

2009年(61)

我的朋友

分类:

2009-08-27 10:20:31

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 – 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.

 

A C  T   - 
 5  -1 -2  -1  -3 
 C  -1  5  -3 -2  -4 
 G  -2  -3  -2 -2 
 T  -1  -2  -2  5  -1
 -  -3  -4  -2  -1  


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 file. 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

2

7 AGTGATG

5 GTTAG

7 AGCTATT

9 AGCTTTAAA

Sample Output

14

21

解题思路

题意:

比较两个基因序列,测定它们的相似度,将两个基因排成直线,如果需要的话插入空格,使基因的长度相等,然后根据那个表格计算出相似度。

 

思路:

DP题,看似跟最长子序列差不多.f[i][j]记录字符串s1中前i个字符与s2中前j个字符的最大匹配值;

考虑f[i][j]

    s1取第i个,s2取第j个, f[i][j] = f[i-1][j-1]+value[m(s1[i])][m(s2[j])];

    s1取第i个,s2’-’ f[i][j] = f[i][j-1]+value[m(s1[i])][m(‘-’)];

    s1’-’s2取第j个, f[i][j] = f[i-1][j]+value[m(‘-’)][m(s2[j])];

f[i][j] 为三者中最大者,状态转移方程:

f[i][j]=max(f[i-1][j-1]+value[m(s1[i])][m(s2[j])],f[i][j-1]+ value[m(s1[i])][m(‘-’)], f[i-1][j]+ value[m(‘-’)][m(s2[j])])

其中m(s1[i]) 为求得s1[i]这个字符相应的数字value[a][b]  (a,b = 0, 1, 2…..) 为分值表里对应的值。整个就是求得两个字符的分值。

 

假如已知AGGT的最大匹配值,AGTGT的最大匹配值,AGGTT的最大匹配值,求AGTGTT的最大匹配值,这个值是AGGT的最大匹配值加上T T的匹配值,AGTGT的最大匹配值加上T -的匹配值,AGGTT的最大匹配值加上-T的匹配值中的最大值,

 

考虑边界条件,即ij0的情况:

                    i=j=0时,根据f[1][1] = f[0][0]+value[m(s1[i])][m(s2[j])],f[0][0] = 0;

                    i=0时,用f[0][j] = f[0][j-1]+ value[m(‘-’)][m(s2[j])]计算

                    j=0时,用f[i][0] = f[i-1][0]+ value[m(s1[i])][m(‘-’)]计算

 

 

源程序

 

#include <stdio.h>
#include <string.h>
#include <conio.h>
#define N 101
int value[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};
int mm(char c)
{
    switch(c)
    {
    case 'A': return 0;
    case 'C': return 1;
    case 'G': return 2;
    case 'T': return 3;
    case '-': return 4;
    }

}
//f[i][j] = max{f[i-1][j-1] + m[s1[i],s2[j]], f[i][j-1] + m['_', s2[j]], f[i-1][j] + m[s1[i], '_']}


int max(int a, int b ,int c)
{
    int m;
    if(a > b)
        m = a;
    else m = b;
    if(c> m)
        m = c;
    return m;
}

void fun(int n, int m, char s1[], char s2[])
{
    int i, j;
    int t1, t2, t3;
    int f[N][N];
    
    f[0][0] = 0;
    for(i=1; i<=n; i++)
        f[i][0] = f[i-1][0] + value[mm(s1[i])][mm('-')];

    for(j=1; j<=m; j++)
        f[0][j] = f[0][j-1] + value[mm('-')][mm( s2[j])];

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            t1 = value[mm(s1[i])][mm(s2[j])] + f[i-1][j-1];
            t2 = f[i][j-1] + value[mm('-')][mm( s2[j])];
            t3 = f[i-1][j] + value[mm(s1[i])][mm('-')];
            f[i][j] = max(t1, t2, t3);
        }
    }
    printf("%d\n", f[n][m]);
}

int main()
{
    int i, t, n, m;
    char gene1[N], gene2[N];

    freopen("in.txt","r",stdin);
    scanf("%d", &t);

    while(t--)
    {
        scanf("%d", &n);
        getchar();
        for(i=1; i<=n; i++)
            scanf("%c", &gene1[i]);

        scanf("%d", &m);
        getchar();
        for(i=1; i<=m; i++)
            scanf("%c", &gene2[i]);

        fun(n, m, gene1, gene2);
    }

    getch();
}

阅读(2258) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-10-29 22:43:12

很久之前的dp啊..... 多谢指正,不过现在实在是不想再回顾这么麻烦的DP,以后有时间再弄吧。

chinaunix网友2009-10-27 23:43:08

写错了, ② s1取第i个,s2用’-’, f[i][j] = f[i][j-1]+value[m(s1[i])][m(‘-’)]; ③ s1用’-’,s2取第j个, f[i][j] = f[i-1][j]+value[m(‘-’)][m(s2[j])];

chinaunix网友2009-08-28 11:39:22

这个写的好详细哦.....顶.....