码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ 1224 DNA Prefix

时间:2018-09-06 14:47:45      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:不用   integer   lightoj   cstring   ini   longest   nbsp   max   Fix   

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

标签:不用   integer   lightoj   cstring   ini   longest   nbsp   max   Fix   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!