-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNAPrefix.java
70 lines (61 loc) · 1.49 KB
/
DNAPrefix.java
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
/** http://lightoj.com/volume_showproblem.php?problem=1224 #trie */
import java.util.Scanner;
class DNAPrefix {
static final int MAX = 4;
static int getIndex(char c) {
int idx = 0;
switch (c) {
case 'A':
idx = 0;
break;
case 'C':
idx = 1;
break;
case 'G':
idx = 2;
break;
case 'T':
idx = 3;
break;
}
return idx;
}
static class TrieNode {
TrieNode[] children;
int countCommonPrefix;
TrieNode() {
children = new TrieNode[MAX];
countCommonPrefix = 0;
}
}
public static int addDNA(TrieNode root, String dna) {
TrieNode pTmp = root;
int length = dna.length();
int idx, ans = 0;
for (int level = 0; level < length; level++) {
idx = getIndex(dna.charAt(level));
if (pTmp.children[idx] == null) {
pTmp.children[idx] = new TrieNode();
}
pTmp = pTmp.children[idx];
pTmp.countCommonPrefix += 1;
ans = Math.max(ans, pTmp.countCommonPrefix * (level + 1));
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
for (int t = 1; t < testcases + 1; t++) {
int n = sc.nextInt();
TrieNode root = new TrieNode();
int ans = 0;
String dna;
for (int i = 0; i < n; i++) {
dna = sc.next();
ans = Math.max(ans, addDNA(root, dna));
}
System.out.println("Case " + t + ": " + ans);
}
}
}