题意:给定n个字符串,求拥有前缀a[0…l]的字符串个数x乘上这个字符串长度,求这个最大值。
思路:建立棵字典树,每个节点纪录上经过这个节点的字符串个数,然后dfs求解。
struct node {
int cnt;
node* nxt[4];
node() {
for (int i = 0;i < 4;++i)
nxt[i] = NULL;
cnt = 0;
}
}*root;
char s[60];
int id(char c) {
if (c == 'A') return 0;
if (c == 'C') return 1;
if (c == 'G') return 2;
return 3;
}
void insert(char* str) {
int len = strlen(str);
node* p = root;
for (int i = 0;i < len;++i) {
int idx = id(str[i]);
if (p->nxt[idx] == NULL) p->nxt[idx] = new node();
p = p->nxt[idx];
p->cnt++;
}
}
void del(node* p) {
if (p == NULL) return ;
for (int i = 0;i < 4;++i)
del(p->nxt[i]);
delete p;
}
int maxNum;
void dfs(node* p, int depth) {
for (int i = 0;i < 4;++i) {
if (p->nxt[i] != NULL) {
dfs(p->nxt[i], depth + 1);
}
}
if (p->cnt * depth > maxNum) {
maxNum = p->cnt * depth;
}
}
int main(int argc, const char * argv[])
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n, t;
cin >> t;
while(t--) {
cin >> n;
root = new node();
for (int i = 0;i < n;++i) {
scanf("%s", s);
insert(s);
}
maxNum = 0;
dfs(root, 0);
printf("Case %d: %d\n", ++nCase, maxNum);
del(root);
}
return 0;
}