zoukankan      html  css  js  c++  java
  • LightOJ 1224

    题目链接:https://cn.vjudge.net/problem/LightOJ-1224

    Given a set of $n$ DNA samples, where each sample is a string containing characters from {A, C, G, T}, we are trying to find a subset of samples in the set, where the length of the longest common prefix multiplied by the number of samples in that subset is maximum.

    To be specific, let the samples be:

    ACGT

    ACGTGCGT

    ACCGTGC

    ACGCCGT

    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.

    Now your task is to report the maximum result we can get from the samples.

    Input
    Input starts with an integer T (≤ 10), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 50000) denoting the number of DNA samples. Each of the next n lines contains a non empty string whose length is not greater than 50. And the strings contain characters from {A, C, G, T}.

    Output
    For each case, print the case number and the maximum result that can be obtained.

    Sample Input
    3

    4

    ACGT

    ACGTGCGT

    ACCGTGC

    ACGCCGT

    3

    CGCGCGCGCGCGCCCCGCCCGCGC

    CGCGCGCGCGCGCCCCGCCCGCAC

    CGCGCGCGCGCGCCCCGCCCGCTC

    2

    CGCGCCGCGCGCGCGCGCGC

    GGCGCCGCGCGCGCGCGCTC

    Sample Output
    Case 1: 9

    Case 2: 66

    Case 3: 20

    Note
    Dataset is huge. Use faster I/O methods.

    题意:

    给出 $n$ 个字符串(只包含四个字符:$A,C,G,T$),让你从中挑选出 $k$ 个字符串,设他们的最长公共前缀的长度为 $L$。求最大的 $k cdot L$。

    题解:

    字典树的每个节点开一个 $pre$ 记录该节点对应的前缀是多少个字符串的前缀。

    然后DFS遍历Trie的每个节点,维护节点深度乘节点 $pre$ 值的最大值即可。时间复杂度等于字典树的空间复杂度。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=5e4+5;
    int n;
    char s[53];
    
    namespace Trie
    {
        const int SIZE=maxn*50;
        int sz;
        struct TrieNode{
            int pre,ed;
            int nxt[4];
        }trie[SIZE];
        void init()
        {
            sz=1;
            memset(trie,0,sizeof(trie));
        }
        inline int idx(const char& c)
        {
            if(c=='A') return 0;
            if(c=='C') return 1;
            if(c=='G') return 2;
            if(c=='T') return 3;
        }
        void insert(const string& s)
        {
            int p=1;
            for(int i=0;i<s.size();i++)
            {
                int ch=idx(s[i]);
                if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
                p=trie[p].nxt[ch];
                trie[p].pre++;
            }
            trie[p].ed++;
        }
    
        int ans;
        void dfs(int d,int now)
        {
            if(!now) return;
            ans=max(ans,d*trie[now].pre);
            for(int i=0;i<4;i++) dfs(d+1,trie[now].nxt[i]);
        }
    };
    
    int main()
    {
        int T;
        cin>>T;
        for(int kase=1;kase<=T;kase++)
        {
            scanf("%d",&n);
            Trie::init();
            for(int i=1;i<=n;i++)
            {
                scanf("%s",s);
                Trie::insert(s);
            }
            Trie::ans=0;
            Trie::dfs(0,1);
            printf("Case %d: %d
    ",kase,Trie::ans);
        }
    }
  • 相关阅读:
    翻转链表——链表
    Hadoop 2.2.0和HBase-0.98 安装snappy
    【POJ 1850】 Code
    Google翻译PDF文档
    leetcode第一刷_Reverse Linked List II
    linux之SQL语句简明教程---AND OR
    spring实战三装配bean之Bean的作用域以及初始化和销毁Bean
    spring实战一:装配bean之注入Bean属性
    系统启动时,spring配置文件解析失败,报”cvc-elt.1: 找不到元素 'beans' 的声明“异常
    阿里云ECS(云服务器)之产品简介
  • 原文地址:https://www.cnblogs.com/dilthey/p/9937396.html
Copyright © 2011-2022 走看看