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;
    }
  • 相关阅读:
    STM32之滴答定时器
    串口--USART1
    风扇--PWM控制
    触摸屏移植Emwin
    emwin--GUI
    extern
    STM32之定时器时间计算
    stm32之定时器
    stm32之PWM
    stm32之SPI
  • 原文地址:https://www.cnblogs.com/kaito77/p/12585400.html
Copyright © 2011-2022 走看看