forked from HarunYOzturk/StringMatching
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreAnalysis.java
More file actions
210 lines (166 loc) · 5.74 KB
/
Copy pathPreAnalysis.java
File metadata and controls
210 lines (166 loc) · 5.74 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//Selman AKSU 21050111015
//Yusif Jabbarzade 21050141026
/**
* 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 textLen = text.length();
int patternLen = pattern.length();
if (patternLen > textLen) {
return "Naive";
}
if (patternLen == 0) {
return "Naive";
}
if (patternLen <= 2) {
return "Naive";
}
if (patternLen <= 4 && textLen < 100) {
return "Naive";
}
double uniqueRatio = calculateUniqueCharacterRatio(pattern);
boolean hasRepeatingPrefix = hasRepeatingPrefix(pattern);
boolean hasHighRepetition = hasHighCharacterRepetition(pattern);
if (hasRepeatingPrefix) {
return "KMP";
}
if (hasHighRepetition && patternLen >= 5) {
return "KMP";
}
if (patternLen >= 15 && textLen > 1000) {
return "RabinKarp";
}
if (patternLen >= 5 && uniqueRatio > 0.6) {
return "BoyerMoore";
}
if (patternLen >= 8 && textLen >= 100) {
return "BoyerMoore";
}
if (patternLen >= 5 && patternLen <= 20) {
return "BoyerMoore";
}
if (patternLen > 20) {
return "RabinKarp";
}
return "BoyerMoore";
}
private double calculateUniqueCharacterRatio(String pattern) {
if (pattern.length() == 0) return 0.0;
Set<Character> uniqueChars = new HashSet<>();
for (char c : pattern.toCharArray()) {
uniqueChars.add(c);
}
return (double) uniqueChars.size() / pattern.length();
}
private boolean hasRepeatingPrefix(String pattern) {
if (pattern.length() < 3) return false;
char firstChar = pattern.charAt(0);
int firstCharCount = 0;
for (int i = 0; i < Math.min(pattern.length(), 5); i++) {
if (pattern.charAt(i) == firstChar) {
firstCharCount++;
}
}
if (firstCharCount >= 3) {
return true;
}
if (pattern.length() >= 4) {
String prefix = pattern.substring(0, 2);
if (pattern.substring(2, 4).equals(prefix)) {
return true;
}
}
return false;
}
private boolean hasHighCharacterRepetition(String pattern) {
if (pattern.length() < 4) return false;
Map<Character, Integer> charCount = new HashMap<>();
for (char c : pattern.toCharArray()) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
int maxCount = 0;
for (int count : charCount.values()) {
maxCount = Math.max(maxCount, count);
}
double repetitionRatio = (double) maxCount / pattern.length();
return repetitionRatio > 0.5;
}
@Override
public String getStrategyDescription() {
return "Hybrid strategy analyzing pattern length, character diversity, " +
"repeating prefix, and character repetition to select optimal algorithm";
}
}
class ExamplePreAnalysis extends PreAnalysis {
@Override
public String chooseAlgorithm(String text, String pattern) {
int textLen = text.length();
int patternLen = pattern.length();
if (patternLen <= 3) {
return "Naive";
} else if (hasRepeatingPrefix(pattern)) {
return "KMP";
} else if (patternLen > 10 && textLen > 1000) {
return "RabinKarp";
} else {
return "Naive";
}
}
private boolean hasRepeatingPrefix(String pattern) {
if (pattern.length() < 2) return false;
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";
}
}
class InstructorPreAnalysis extends PreAnalysis {
@Override
public String chooseAlgorithm(String text, String pattern) {
return null;
}
@Override
public String getStrategyDescription() {
return "Instructor's testing implementation";
}
}