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

    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.

    还是套用字典树的模板,稍微不同的地方是这题求前缀长度×字符串个数的最大值

    那么再开一个数组pr来储存前缀长度

    vis数组表示有多少个字符串共用此前缀

    最大值ans = pr[i[*vis[i]

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    #define N 1000010   //数组开大一点,否则runtime error
    int tire[N][4];
    int vis[N];
    char s[60];
    int pr[N];
    int k = 1, ans;
    
    int change(char s){
        if(s == 'A') return 0;
        else if(s == 'G') return 1;
        else if(s == 'C') return 2;
        else return 3;
    }
    
    void build(){
        int n = strlen(s);
        int p = 0;
        for(int i = 0; i<n; i++){
        int c = change(s[i]);
        if(!tire[p][c]){
            tire[p][c] = k;
            pr[k] = i + 1;
            k++;
        }
        p = tire[p][c];
        vis[p]++;
        }
    }
    
    int main()
    {
        int t, n, te;
        scanf("%d", &t);
        te = t;
        while(t--){
            ans = 0;
            k = 1;
            memset(vis, 0, sizeof(vis));
            memset(tire, 0, sizeof(tire));
            scanf("%d", &n);
            while(n--){
                scanf("%s", s);
                build();
            }
        for(int i = 1; i<k; i++){
            ans = max(ans, pr[i]*vis[i]);
        }
        printf("Case %d: %d
    ", te - t, ans);
        }
        return 0;
    }
  • 相关阅读:
    无法定位程序输入点
    推荐一款免费看小说神器-连尚读书
    APP隐私权限审核规范
    移动互联网应用程序(APP) 启动屏广告行为规范
    [原创]A/B测试不是万能的,但没有是不行的?
    [原创]我的抖音号mayingbao,欢迎来交流
    [原创] push如何提高产品留存?
    [原创]创业公司中App专用术语
    [原创]app应用上架申请所需材料
    [原创]这37款APP,下架!做为测试人员,你应了解些这方面知识
  • 原文地址:https://www.cnblogs.com/kaito77/p/12585400.html
Copyright © 2011-2022 走看看