Light OJ 1224 字典树

http://lightoj.com/volume_showproblem.php?problem=1224

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

Output for Sample Input

3

4

ACGT

ACGTGCGT

ACCGTGC

ACGCCGT

3

CGCGCGCGCGCGCCCCGCCCGCGC

CGCGCGCGCGCGCCCCGCCCGCAC

CGCGCGCGCGCGCCCCGCCCGCTC

2

CGCGCCGCGCGCGCGCGCGC

GGCGCCGCGCGCGCGCGCTC

Case 1: 9

Case 2: 66

Case 3: 20

题目大意:给出n个只由A、C、G、T组成的字符串序列,设一个字符串为s, 字符串序列中以s为前缀的数目与s的长度的乘积的最大值。

思路:字典树,用sum[i]表示以从根节点0到节点i连成的字符串为前缀的数目,在Insert的同时用MAX维护:MAX=max(MAX,sum[i]*len)即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#define INF 0x3f3f3f3f
using namespace std;

map<char,int> m;//映射 A G C T 对应的数字
int tree[2000000][4];
int sum[2000000];
int MAX=0;
int tot;
char s[55];

void Insert()
{
	int len=strlen(s);
	int root=0;
	for(int i=0;i<len;i++)
	{
		int id=m[s[i]];//得到id
		if(!tree[root][id])
			tree[root][id]=++tot;
		++sum[tree[root][id]];
		MAX=max(MAX,sum[tree[root][id]]*(i+1));
		root=tree[root][id];
	}
}

int main()
{
	m['A']=0;
	m['G']=1;
	m['C']=2;
	m['T']=3;
	int t;
	scanf("%d",&t);
	int times=0;
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",s);
			Insert();
		}
		printf("Case %d: %d\n",++times,MAX);
		memset(tree,0,(tot+1)*sizeof(tree[0]));
		memset(sum,0,(tot+1)*sizeof(int));
		MAX=tot=0;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/88761857