本题题意是让你找出一个子字符串,使该字符串作为所有插入字符串前缀的次数 * 该字符串的长度 结果最大
所以我们可以在插入字符串的时候进行统计,用一个maxx和sum[root] ∗ 当前长度len 比较取大即可
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2e6 + 10;
int tire[maxn][4];
int sum[maxn];
int maxx = 0;
int tot = 0;
void insert(char *s){
int len = strlen(s);
int rt = 0;
for(int i = 0; i < len; ++ i){
int id = 0;
if(s[i] == 'C')
id = 1;
else if(s[i] == 'G')
id = 2;
else if(s[i] == 'T')
id = 3;
if(tire[rt][id] == 0)
tire[rt][id] = ++ tot;
rt = tire[rt][id];
sum[rt] ++;
maxx = max(maxx, sum[rt] * (i + 1));
}
}
int n, t;
char s[52];
int main(){
int cnt = 1;
for(scanf("%d", &t); t; --t){
scanf("%d", &n);
tot = 0;
maxx = 0;
for(int i = 0; i < n; ++ i){
scanf("%s", s);
insert(s);
}
printf("Case %d: %d\n", cnt++, maxx);
for(int i = 0; i <= tot; ++ i){
sum[i] = 0;
for(int j = 0; j < 4; ++ j)
tire[i][j] = 0;
}
}
}