Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320577
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-23 11:26:34

基因序列相似性问题

Time Limit:5sMemory limit:32M
Accepted Submit:47Total Submit:451

Genotype 是一个有限的基因序列集。它的每个成员都是由大写的英文字母A-Z组成,不同的字母表示不同种类的基因。一个基因种类可以分化成为若干新的基因种类。这种 分化一般呈树状结构。树根处的基因序列称为母序列。基因序列中含有母序列的基因子序列称为本质基因子序列。生物信息学家们在研究Genotype 基因序列时,需要研究同一种类基因序列的相似性。对于同一种类的2个基因序列X和Y,已知它们的母序列P,基因序列X和Y的最长公共本质基因子序列给出其 相似性的准确刻画。为了有效地分析基因序列的相似性,科学家们希望设计出一个高效的计算程序,能对给定的基因序列X,Y和它们的母序列P,快速计算出基因 序列X和Y的最长公共本质基因子序列的长度。

编程任务:
给定基因序列X,Y和母序列P,计算出基因序列X和Y的最长公共本质基因子序列的长度。
数据输入:
输入数据的前3行中每行有一个正整数,分别表示序列X,Y和P的长度m,n和r(1≤m,n,r≤1000)。 接下来的3行给出序列X,Y和P。

结果输出:
在屏幕上输出基因序列X和Y的最长公共本质基因子序列的长度。如果基因序列X和Y没有以P为母序列的公共本质基因子序列,则输出0。

输入示例

3
3
1
ABC
BCA
A

输出示例

1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_VAL(x, y) (((x) > (y)) ? (x) : (y))
#define MIN_VAL(x, y) (((x) < (y)) ? (x) : (y))

int lgen1, lgen2, lgenc;
char gen1[1010];
char gen2[1010];
char genc[1010];

int lca[2][1010][1010];

int main()
{
    int i, j, k;
    int point;
    int flag, a, b, ta, tb;

    while (scanf("%d %d %d", &lgen1, &lgen2, &lgenc) != EOF)
    {
        scanf("%s %s %s", &gen1[1], &gen2[1], &genc[1]);
        
        point = 0;
        for (i = 0; i <= lgen1; ++i)
        {
            for (j = 0; j <= lgen2; ++j)
                lca[point][i][j] = 0;
        }

        for (i = 1; i <= lgen1; ++i)
        {
            for (j = 1; j <= lgen2; ++j)
            {
                if (gen1[i] == gen2[j])
                    lca[point][i][j] = lca[point][i - 1][j - 1] + 1;
                else
                    lca[point][i][j] = MAX_VAL(lca[point][i][j - 1], lca[point][i - 1][j]);
            }
        }

        flag = 1;
        a = b = 1;
        for (k = 1; k <= lgenc && flag; ++k)
        {
            flag = 0;
            ta = 1000000;
            tb = 1000000;
            for (i = a - 1; i <= lgen1; ++i)
            {
                for (j = b - 1; j <= lgen2; ++j)
                    lca[1 - point][i][j] = -1;
            }
            for (i = a; i <= lgen1; ++i)
            {
                for (j = b; j <= lgen2; ++j)
                {
                    if (gen1[i] == gen2[j] && gen1[i] == genc[k])
                    {
                        if (k == 1)
                        {
                            lca[1 - point][i][j] = lca[point][i - 1][j - 1] + 1;
                            flag = 1;
                            ta = MIN_VAL(i, ta);
                            tb = MIN_VAL(j, tb);
                        }
                        else
                        {
                            if (lca[point][i - 1][j - 1] == -1)
                                lca[1 - point][i][j] = -1;
                            else
                            {
                                flag = 1;
                                ta = MIN_VAL(i, ta);
                                tb = MIN_VAL(j, tb);
                                lca[1 - point][i][j] = lca[point][i - 1][j - 1] + 1;
                            }
                        }
                    }
                    else if (gen1[i] == gen2[j])
                    {
                        if (lca[1 - point][i - 1][j - 1] == -1)
                            lca[1 - point][i][j] = -1;
                        else
                            lca[1 - point][i][j] = lca[1 - point][i - 1][j - 1] + 1;
                    }
                    else
                    {
                        if (lca[1 - point][i][j - 1] == -1 && lca[1 - point][i - 1][j] == -1)
                            lca[1 - point][i][j] = -1;
                        else
                            lca[1 - point][i][j] = MAX_VAL(lca[1 - point][i][j - 1], lca[1 - point][i - 1][j]);
                    }
                }
            }
            point = 1 - point;
            a = ta;
            b = tb;
        }

        printf("%d\n", lca[point][lgen1][lgen2] < 0 ? 0 : lca[point][lgen1][lgen2]);
    }

    return 0;
}

阅读(2545) | 评论(0) | 转发(0) |
0

上一篇:Binary Search Tree II

下一篇:Increasing Sequences

给主人留下些什么吧!~~