一开始蠢了,想着直接看最后一个,其实直接边插边搞就好了~
#include<iostream>
#include<vector>
#include<cstring>
#include<map>
using namespace std;
using ll = long long;
const int M = 2e6+10;
struct Trie{
int x[10],num;
}tree[M];
int cnt = 1;
int res;
map<char,int>mp;
void pre()
{
for(int i=0;i<=cnt;i++){
for(int j=0;j<=8;++j)tree[i].x[j] = 0;
tree[i].num = 0;
}
cnt = 1;
}
int getnum(char c){
return mp[c];
}
void insert(string t)
{
int pos = 1;
int len = t.size();
for(int i=0;i<len;++i){
int num = getnum(t[i]);
if(!tree[pos].x[num])tree[pos].x[num] = ++cnt;
pos = tree[pos].x[num];
tree[pos].num++;
res = max(tree[pos].num*(i+1),res);
}
}
int main()
{
mp['A'] = 1;
mp['C'] = 2;
mp['G'] = 3;
mp['T'] = 4;
int _;cin>>_;
int ct = 0;
while(_--){
pre();
ct++;
res = 0;
int t;cin>>t;
for(int i=1;i<=t;++i){
string s;cin>>s;
insert(s);
if(i==t)
printf("Case %d: %d\n",ct,res);
}
}
}