LightOJ - 1224 DNA Prefix trie字典树 毒瘤题

DNA Prefix

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
这题是求多组字符串的子集的最长公共前缀和长度的乘积,题目不难,cut[u]保存的就是以u结尾的前缀的出现次数,所以每次处理字符串的时候更新一下最大值就可以了
可是这题我少说也re、wa了十几发!!!这编译器不知道哪里抽风,竟然对const和宏定义有什么奇怪的问题,所以我只好改用c++写,没想到,题目中说50000个串,我开了2500000个才够用!!坑爹啊!!

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

int trie[2500000][5];
int cnt[2500000]; 
int cnts=0;
long long sz,ans=0;
int t,n; char s[600];


void init()
{
	sz=1;
	memset(cnt,0,sizeof(cnt));
	memset(trie[0],0,sizeof(trie));
}

int getid(char ch)
{

		if(ch=='A') return 1;
		if(ch=='C') return 2;
		if(ch=='G')	return 3;
		if(ch=='T') return 4;
}

void build(char s[])
{
	int u=0;
	int len=strlen(s);
	for(int i=0;i<len;i++)
	{
		char ch = getid(s[i]);
		if(!trie[u][ch])
		{
			memset(trie[sz],0,sizeof(trie[sz]));
			trie[u][ch]=sz++;
		}
		u=trie[u][ch];
		cnt[u]++;
	
		if(cnt[u]*(i+1)>ans)
			ans=cnt[u]*(i+1);//cnt[u] 保存以当前结尾的前缀的出现次数
	}
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		ans=0;
		init();
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",s);

			build(s);
		}
	
		printf("Case %d: %lld\n",++cnts,ans);
	
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值