-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreAnalysis.java
More file actions
148 lines (127 loc) · 4.8 KB
/
Copy pathPreAnalysis.java
File metadata and controls
148 lines (127 loc) · 4.8 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* PreAnalysis interface for students to implement their algorithm selection logic
*
* Students should analyze the characteristics of the text and pattern to determine
* which algorithm would be most efficient for the given input.
*
* The system will automatically use this analysis if the chooseAlgorithm method
* returns a non-null value.
*/
public abstract class PreAnalysis {
/**
* Analyze the text and pattern to choose the best algorithm
*
* @param text The text to search in
* @param pattern The pattern to search for
* @return The name of the algorithm to use (e.g., "Naive", "KMP", "RabinKarp", "BoyerMoore", "GoCrazy")
* Return null if you want to skip pre-analysis and run all algorithms
*
* Tips for students:
* - Consider the length of the text and pattern
* - Consider the characteristics of the pattern (repeating characters, etc.)
* - Consider the alphabet size
* - Think about which algorithm performs best in different scenarios
*/
public abstract String chooseAlgorithm(String text, String pattern);
/**
* Get a description of your analysis strategy
* This will be displayed in the output
*/
public abstract String getStrategyDescription();
}
/**
* Default implementation that students should modify
* This is where students write their pre-analysis logic
*/
class StudentPreAnalysis extends PreAnalysis {
@Override
public String chooseAlgorithm(String text, String pattern) {
int n = text.length();
int m = pattern.length();
// Case 1: If the pattern is longer than the text or empty
if (m > n || m == 0) {
return "Naive";
}
// Case 2: If the pattern is very short (1-2 letters)
if (m <= 2) {
return "Naive";
}
// Case 3: Alphabet analysis (Is it a small character set like DNA or Binary?)
boolean isSmallAlphabet = true;
int checkLimit = Math.min(n, 20);
for (int i = 0; i < checkLimit; i++) {
char c = text.charAt(i);
if ("ACGT01 \t\n".indexOf(c) == -1) {
isSmallAlphabet = false;
break;
}
}
// 4. Strategic Decision Making
if (isSmallAlphabet) {
// KMP is very good for structures with repetitions and small alphabets like DNA.
return "KMP";
} else if (m > 10) {
// If the pattern is long and the alphabet is large (like normal English text)
// GoCrazy (Sunday Algorithm) makes great jumps.
return "GoCrazy";
} else {
// Boyer-Moore is reliable for other standard cases.
return "BoyerMoore";
}
}
@Override
public String getStrategyDescription() {
return "My Strategy: Using Naive for short patterns, KMP for small alphabets (DNA), GoCrazy (Sunday) for long patterns, and BoyerMoore for other cases.";
}
}
/**
* Example implementation showing how pre-analysis could work
* This is for demonstration purposes
*/
class ExamplePreAnalysis extends PreAnalysis {
@Override
public String chooseAlgorithm(String text, String pattern) {
int textLen = text.length();
int patternLen = pattern.length();
// Simple heuristic example
if (patternLen <= 3) {
return "Naive"; // For very short patterns, naive is often fastest
} else if (hasRepeatingPrefix(pattern)) {
return "KMP"; // KMP is good for patterns with repeating prefixes
} else if (patternLen > 10 && textLen > 1000) {
return "RabinKarp"; // RabinKarp can be good for long patterns in long texts
} else {
return "Naive"; // Default to naive for other cases
}
}
private boolean hasRepeatingPrefix(String pattern) {
if (pattern.length() < 2) return false;
// Check if first character repeats
char first = pattern.charAt(0);
int count = 0;
for (int i = 0; i < Math.min(pattern.length(), 5); i++) {
if (pattern.charAt(i) == first) count++;
}
return count >= 3;
}
@Override
public String getStrategyDescription() {
return "Example strategy: Choose based on pattern length and characteristics";
}
}
/**
* Instructor's pre-analysis implementation (for testing purposes only)
* Students should NOT modify this class
*/
class InstructorPreAnalysis extends PreAnalysis {
@Override
public String chooseAlgorithm(String text, String pattern) {
// This is a placeholder for instructor testing
// Students should focus on implementing StudentPreAnalysis
return null;
}
@Override
public String getStrategyDescription() {
return "Instructor's testing implementation";
}
}