/*
每个节点经过的次数乘上该节点到跟节
点的距离,这些数中的最大值即为答案
*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
node* next[4];
int count, num;
node(){
for(int i = 0; i < 4; i++) next[i] = NULL;
count = num = 0;
}
~node(){for(int i = 0; i < 4; i++) if(next[i]) delete next[i];}
};
int maxn;
int get(char s){
if(s == 'A')return 0;
if(s == 'C')return 1;
if(s == 'G')return 2;
if(s == 'T')return 3;
}
void insert(node* root, char s[]){
int n = strlen(s);
node* p = root;
for(int i = 0; i < n; i++){
int index = get(s[i]);
if(p->next[index] == NULL){
p->next[index] = new node;
p->next[index]->count = 1;
p->next[index]->num = (p->num)+1;
}
else
p->next[index]->count++;
p = p->next[index];
}
}
void dfs(node* root){
for(int i = 0; i < 4; i++){
if(root->next[i]){
maxn = max(maxn, (root->next[i]->count)*(root->next[i]->num));
dfs(root->next[i]);
}
}
}
int main()
{
int t, cas = 1, n;
char s[60];
cin >> t;
while(t--)
{
node* root = new node;
cin >> n;
while(n--){
scanf("%s",s);
insert(root, s);
}
maxn = 0;
dfs(root);
printf("Case %d: %d\n",cas++, maxn);
delete root;
}
return 0;
}
light oj 1224
最新推荐文章于 2019-03-30 20:32:11 发布