首页 > 其他 > 详细

LightOJ 1224 DNA Prefix

时间:2018-09-06 13:43:18      阅读:170      评论:0      收藏:0      [点我收藏+]

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.

 

 

字典树,不过一共只有四个字母,所以映射四个连续位置,不用每个点都有26个儿子。

代码:

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
using namespace std;
int n,m,t,pos;
int trie[2000001][4],pre[2000001];
int to[26];
void Insert(string s) {
    int i = 0,c = 0,len = s.size();
    while(i < len) {
        int d = to[s[i] - A];
        if(!trie[c][d]) {
            trie[c][d] = ++ pos;
        }
        c = trie[c][d];
        pre[c] ++;
        m = max(m,pre[c] * (i + 1));
        i ++;
    }
}
int main() {
    to[C - A] = 1;
    to[G - A] = 2;
    to[T - A] = 3;
    scanf("%d",&t);
    char s[51];
    for(int i = 1;i <= t;i ++) {
        memset(pre,0,sizeof(pre));
        m = 0;
        scanf("%d",&n);
        for(int j = 0;j < n;j ++) {
            scanf("%s",s);
            Insert(s);
        }
        printf("Case %d: %d\n",i,m);
    }
}

 

LightOJ 1224 DNA Prefix

原文:https://www.cnblogs.com/8023spz/p/9597545.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!