loj 1224 - DNA Prefix

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

题目链接


题目描述很简单  有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;
}
目录
相关文章
|
12月前
|
机器学习/深度学习 自然语言处理 语音技术
从 Seq2Seq 到 Attention:彻底改变序列建模
从 Seq2Seq 到 Attention:彻底改变序列建模
57 0
|
5月前
|
缓存 数据可视化 关系型数据库
单细胞分析:多模态 reference mapping (2)
单细胞分析:多模态 reference mapping (2)
84 4
|
2月前
|
机器学习/深度学习 文字识别 自然语言处理
OCR -- 文本识别 -- 理论篇
OCR -- 文本识别 -- 理论篇
58 0
|
5月前
|
数据可视化 atlas
单细胞分析:多模态 reference mapping (1)
单细胞分析:多模态 reference mapping (1)
56 1
|
5月前
|
人工智能 测试技术 API
[译][AI OpenAI-doc] 速率限制
速率限制是我们的API对用户或客户在指定时间段内访问我们服务的次数施加的限制。速率限制是API的一种常见做法,有助于防止对API的滥用或误用,并确保每个人都能公平地访问API。本文介绍了速率限制的原因、工作方式以及如何处理速率限制错误。
|
5月前
|
机器学习/深度学习 人工智能 自然语言处理
NeurIPS’23 Paper Digest | 如何把 LLM 的推理能力应用于事件序列预测?
我们完成了首个把 LLM 推理能力引入事件序列领域的工作。代码、数据均已经开源,并将集成进开源库 EasyTPP。
NeurIPS’23 Paper Digest | 如何把 LLM 的推理能力应用于事件序列预测?
二代测序fastq序列名称格式(illumina NGS)
二代测序fastq序列名称格式(illumina NGS)
|
机器学习/深度学习 存储 人工智能
7 Papers & Radios | BERT上下文长度达200万token;华人团队通用分割模型SEEM
7 Papers & Radios | BERT上下文长度达200万token;华人团队通用分割模型SEEM
192 0
|
机器学习/深度学习 人工智能 开发框架
ACL2022论文分享-MDERank:关键词提取方法及其预训练模型
ACL2022论文分享-MDERank:关键词提取方法及其预训练模型
4703 0
ACL2022论文分享-MDERank:关键词提取方法及其预训练模型