-
Notifications
You must be signed in to change notification settings - Fork 1
/
bwmatching.java
79 lines (70 loc) · 3.19 KB
/
bwmatching.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
71
72
73
74
75
76
77
78
79
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BWMatching {
class FastScanner {
StringTokenizer tok = new StringTokenizer("");
BufferedReader in;
FastScanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (!tok.hasMoreElements())
tok = new StringTokenizer(in.readLine());
return tok.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
// Preprocess the Burrows-Wheeler Transform bwt of some text
// and compute as a result:
// * starts - for each character C in bwt, starts[C] is the first position
// of this character in the sorted array of
// all characters of the text.
// * occ_count_before - for each character C in bwt and each position P in bwt,
// occ_count_before[C][P] is the number of occurrences of character C in bwt
// from position 0 to position P inclusive.
private void PreprocessBWT(String bwt, Map<Character, Integer> starts, Map<Character, int[]> occ_counts_before) {
// Implement this function yourself
}
// Compute the number of occurrences of string pattern in the text
// given only Burrows-Wheeler Transform bwt of the text and additional
// information we get from the preprocessing stage - starts and occ_counts_before.
int CountOccurrences(String pattern, String bwt, Map<Character, Integer> starts, Map<Character, int[]> occ_counts_before) {
// Implement this function yourself
}
static public void main(String[] args) throws IOException {
new BWMatching().run();
}
public void print(int[] x) {
for (int a : x) {
System.out.print(a + " ");
}
System.out.println();
}
public void run() throws IOException {
FastScanner scanner = new FastScanner();
String bwt = scanner.next();
// Start of each character in the sorted list of characters of bwt,
// see the description in the comment about function PreprocessBWT
Map<Character, Integer> starts = new HashMap<Character, Integer>();
// Occurrence counts for each character and each position in bwt,
// see the description in the comment about function PreprocessBWT
Map<Character, int[]> occ_counts_before = new HashMap<Character, int[]>();
// Preprocess the BWT once to get starts and occ_count_before.
// For each pattern, we will then use these precomputed values and
// spend only O(|pattern|) to find all occurrences of the pattern
// in the text instead of O(|pattern| + |text|).
PreprocessBWT(bwt, starts, occ_counts_before);
int patternCount = scanner.nextInt();
String[] patterns = new String[patternCount];
int[] result = new int[patternCount];
for (int i = 0; i < patternCount; ++i) {
patterns[i] = scanner.next();
result[i] = CountOccurrences(patterns[i], bwt, starts, occ_counts_before);
}
print(result);
}
}