zoukankan      html  css  js  c++  java
  • LightOJ 1224 DNA Prefix

    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.

    字典树,保证空间充足否则re。

    代码:

        #include <iostream>
        #include <cstdio>
        #include <sstream>
        #include <cstring>
        using namespace std;
        int n,t,pos;
        int trie[800001][4];
        int num[800001];
        int ans;
        int to[90];
        void Insert(char *s) {
            int i = 0,c = 0;
            while(s[i]) {
                int d = to[s[i]];
                if(!trie[c][d]) {
                    trie[c][d] = ++ pos;
                }
                c = trie[c][d];
                ans = max(ans,++ num[c] * ++ i);
            }
        }
        int main() {
            to['A'] = 0;
            to['C'] = 1;
            to['G'] = 2;
            to['T'] = 3;
            scanf("%d",&t);
            char s[51];
            for(int i = 1;i <= t;i ++) {
                scanf("%d",&n);
                pos = 0;
                memset(num,0,sizeof(num));
                memset(trie,0,sizeof(trie));
                ans = 0;
                for(int j = 0;j < n;j ++) {
                    scanf("%s",s);
                    Insert(s);
                }
                printf("Case %d: %d
    ",i,ans);
            }
        }
  • 相关阅读:
    重链剖分的总结与模板
    PBDS学习笔记(一)
    LCT 第一题 洛谷模板
    2018年暑假第四次周赛-图论部分题解
    后缀数组求不同子串的个数
    Codeforces Round #106 (Div. 2) Coloring Brackets(区间DP)
    Codeforces Round #510 (Div. 2) D. Petya and Array (权值线段树)
    HDU 3974 Assign the task (dfs序+线段树)
    Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D.Valid BFS? (模拟)
    POJ
  • 原文地址:https://www.cnblogs.com/8023spz/p/9597545.html
Copyright © 2011-2022 走看看