forked from shiningflash/Advance-Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightOJ_1224.cpp
71 lines (64 loc) · 1.39 KB
/
lightOJ_1224.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
map <char, int> mp;
struct node {
int cnt;
bool mark;
node *next[4];
node() {
cnt = 0;
mark = false;
for (int i = 0; i < 4; next[i] = NULL, i++);
}
} * root;
void insert(string s) {
node *cur = root;
for (int i = 0; s[i]; i++) {
int id = mp[s[i]];
if (cur->next[id] == NULL) cur->next[id] = new node();
cur = cur->next[id];
cur->cnt++;
}
}
int search(string s) {
node *cur = root;
int mx = -1;
for (int i = 0; s[i]; i++) {
int id = mp[s[i]];
cur = cur->next[id];
int height = i+1;
int tmp = cur->cnt * height;
if (tmp > mx) mx = tmp;
}
return mx;
}
void del(node* cur) {
for (int i = 0; i < 4; i++)
if (cur->next[i]) del(cur->next[i]);
delete(cur);
}
int main() {
mp['A'] = 0;
mp['C'] = 1;
mp['G'] = 2;
mp['T'] = 3;
int tst, t = 1, n, q;
string s;
for (scanf("%d", &tst); tst--;) {
scanf("%d", &n);
root = new node();
vector <string> v;
while (n--) {
cin >> s;
insert(s);
v.push_back(s);
}
int mx = -1;
for (int i = 0; i < v.size(); i++) {
int tmp = search(v[i]);
if (tmp > mx) mx = tmp;
}
printf("Case %d: %d\n", t++, mx);
del(root);
}
}