loj 1224 - DNA Prefix

题目链接

题目描述很简单  有n和DNA序列,求出他们中公共前缀长度和有相同公共前缀DNA序列乘积的最大值。

If we take the subset {ACGT} then the result is 4 (4 * 1), if we take {ACGT, ACGTGCGT, ACGCCGT} then the result is 3 * 3 = 9 (since ACG is the common prefix), if we take {ACGT, ACGTGCGT, ACCGTGC, ACGCCGT} then the result is 2 * 4 = 8.

    这个就是英文的描述,很简单,就不翻译了。

思路:

     这个题目明显用trie做,我是这样想的,用trie存储所有的DNA序列,然后每个节点有个cnt值,表示的是存储以该节点结尾的DNA序列的数目。

     在输入过程中便开始建树的过程,最后,在查询结果的时候我用了递归的方式,代码比较好写,也容易理解。

     还有最后一点,虽然我们在竞赛中很少使用指针,但涉及到指针,在用完后一定要释放,否则会MLE,我就MLE两次。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct trie
{
    struct trie *next[5];
    int cnt;
} *p;

trie* newtrie()
{
    trie *q = new trie();
    for (int i = 0; i < 4; i++)
        q->next[i] = NULL;
    q->cnt = 0;
    return q;
};
void insert(char *str, trie *root)
{
    p = root;
    while (*str)
    {
        int pos;
        if (*str == 'A')
            pos = 0;
        else if (*str == 'C')
            pos = 1;
        else if (*str == 'G')
            pos = 2;
        else
            pos = 3;
        if (!p->next[pos])
        {
            p->next[pos] = newtrie();
            p = p->next[pos];
            p->cnt++;
        }
        else
        {
            p = p->next[pos];
            p->cnt++;
        }
        str++;
    }
}

int maxnum(struct trie *q, int d)
{
    int maxn = d * q->cnt;
    for (int i = 0; i < 4; i++)
    {
        if (q->next[i])
        {
            maxn = max(maxn, maxnum(q->next[i], d+1));
        }
    }
    return maxn;
}
void moveit(trie *root)
{
    if (root == NULL)
        return;
    for (int i = 0; i < 4;i++)
    {
        moveit(root->next[i]);
    }
    free(root);
}

int main()
{
    int t, n;
    char s[55];
    scanf("%d",&t);
    for(int k = 1; k <= t; k++)
    {
        trie *root = newtrie();
        scanf("%d",&n);
        while (n--)
        {
            scanf("%s",s);
            insert(s, root);
        }
        printf("Case %d: %d\n", k, maxnum(root, 0));
        moveit(root);
    }
    return 0;
}

转载于:https://www.cnblogs.com/xindoo/archive/2013/04/20/3595113.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值