DNA Prefix (Light_1224) 动态字典树 + 模板题

来源:互联网 发布:csgo职业选手优化fps 编辑:程序博客网 时间:2024/05/21 17:05
N - DNA Prefix
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Submit Status Practice LightOJ 1224

Description

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


题目大意:给出一些DNA序列片段,求出这些片段   重复片段数与共同前缀数;


解题思路:动态字典树。在建树的同时更新答案。


代码如下:


#include"iostream"#include"cstring"#include"cstdio"using namespace std;const int maxn = 4;int ans;typedef struct node{node *next[maxn];int cnt;}node,*trie; trie T; int transf(char c){if(c == 'A') return 0;if(c == 'C') return 1;if(c == 'G') return 2;if(c == 'T') return 3;}int max(int x,int y){return x > y ? x : y;}node *newNode(){ //生成一个节点 node *temp = new node;temp -> cnt =0; for(int i = 0;i < maxn;i ++){temp -> next[i] = NULL;}return temp;}void creat(char *str){ //创建字典树 node *p = T;int t,len = strlen(str);for(int i = 0,id = 0;i < len;i ++){id = transf(str[i]);if(p -> next[id] == NULL){p -> next[id] = newNode(); }p = p -> next[id];p -> cnt ++; //在创建同时不断更新ans的值 t = (i + 1) * p -> cnt;ans = max(t,ans);}}void freedom(node *p){for(int i = 0;i < maxn;i ++){if(p -> next[i] != NULL)freedom(p -> next[i]);}delete(p);}int main(){int C,n;char str[55];scanf("%d",&C);for(int k = 1;k <= C;k ++){scanf("%d",&n);T = newNode();ans = 0;while(n --){scanf("%s",str);creat(str);}printf("Case %d: %d\n",k,ans);freedom(T);}return 0;} 


0 0
原创粉丝点击