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
这道题目大意是给你若干条核苷酸链你要计算出公共前缀长度乘以具有公共前缀的链条的数目的最大值,我看别的大犇写的博客都需要trie的删除操作,我仔细想了一下发现并不需要这么做,我们这只要加入一个数组来不断地维护我们的最大值就行了
以下是AC代码.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
const int MAXN = 2500000;
int trie[MAXN][5], num[MAXN], tot, rt, ans = -99;
int getid(char s)//以此操作来节省空间
{
switch(s)
{
case 'A' : return 1; break;
case 'C' : return 2; break;
case 'G' : return 3; break;
case 'T' : return 4; break;
}
}
void Insert_trie(char *data)
{
int len = strlen(data);
rt = 0;
for(int i = 0; i < len; ++i)
{
int y = getid(data[i]);
if(trie[rt][y] == 0)
{
trie[rt][y] = ++tot;
}
rt = trie[rt][y];
num[rt]++;
if(num[rt]* (i + 1) > ans)//维护最大值
{
ans = num[rt] * (i + 1);
}
}
}
int main()
{
int t, n;
char word[601];
cin >> t;
for(int i = 1; i <= t; ++i)
{
memset(num, 0, sizeof(num));
memset(trie, 0, sizeof(trie));
cin >> n;
ans = -99;
for(int i = 0; i < n; i++)
{
cin >> word;
Insert_trie(word);
}
printf("Case %d: %d\n", i, ans);
}
}