-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
477 lines (401 loc) · 10.8 KB
/
Copy pathMain.java
File metadata and controls
477 lines (401 loc) · 10.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import java.io.*;
import java.math.BigInteger;
import java.util.*;
/**
*
* @author Saju
*
*/
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
/*
*/
out.flush();
out.close();
System.exit(0);
}
private static void testTrie(){
Trie trie = new Trie();
trie.insert("Programming");
trie.insert("is");
trie.insert("a");
trie.insert("way");
trie.insert("of");
trie.insert("life");
System.out.println(trie.containsNode("is"));
trie.delete("is");
System.out.println(trie.containsNode("is"));
}
private static class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
current = current.getChildren().computeIfAbsent(word.charAt(i), c -> new TrieNode());
}
current.setEndOfWord(true);
}
boolean delete(String word) {
return delete(root, word, 0);
}
boolean containsNode(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = current.getChildren().get(ch);
if (node == null) {
return false;
}
current = node;
}
return current.isEndOfWord();
}
boolean isEmpty() {
return root == null;
}
private boolean delete(TrieNode current, String word, int index) {
if (index == word.length()) {
if (!current.isEndOfWord()) {
return false;
}
current.setEndOfWord(false);
return current.getChildren().isEmpty();
}
char ch = word.charAt(index);
TrieNode node = current.getChildren().get(ch);
if (node == null) {
return false;
}
boolean shouldDeleteCurrentNode = delete(node, word, index + 1) && !node.isEndOfWord();
if (shouldDeleteCurrentNode) {
current.getChildren().remove(ch);
return current.getChildren().isEmpty();
}
return false;
}
}
private static class TrieNode {
private final Map<Character, TrieNode> children = new HashMap<>();
private boolean endOfWord;
Map<Character, TrieNode> getChildren() {
return children;
}
boolean isEndOfWord() {
return endOfWord;
}
void setEndOfWord(boolean endOfWord) {
this.endOfWord = endOfWord;
}
}
private static int maxMatch(int n, int m, List<Integer>[] list) {
int matching[] = new int[m + 1];
boolean visited[] = new boolean[n + 1];
Arrays.fill(matching, -1);
int match = 0;
for(int i = 0; i < n; i++){
Arrays.fill(visited, false);
if(hasPath(i, n, m, list, visited, matching)){
match++;
}
}
return match;
}
private static boolean hasPath(int i, int n, int m, List<Integer>[] list, boolean[] visited, int[] matching) {
visited[i] = true;
for(int v : list[i]){
int j = matching[v];
if(j == -1 || (!visited[j] && hasPath(j, n, m, list, visited, matching))){
matching[v] = i;
return true;
}
}
return false;
}
private static int log(int x, int base) {
return (int) (Math.log(x) / Math.log(base));
}
private static long max(long a, long b) {
if (a >= b) {
return a;
}
return b;
}
private static long min(long a, long b) {
if (a <= b) {
return a;
}
return b;
}
private static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// private static long bigMod(long n, long k, long m) {
//
// long ans = 1;
// while (k > 0) {
// if ((k & 1) == 1) {
// ans = (ans * n) % m;
// }
// n = (n * n) % m;
// k >>= 1;
// }
// return ans;
// }
/*
* Returns an iterator pointing to the first element in the range [first,
* last] which does not compare less than val.
*
*/
private static int lowerBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid >= 0 && arr[mid] == num) {
mid--;
}
return mid + 1;
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
/*
* upper_bound() is a standard library function in C++ defined in the header
* . It returns an iterator pointing to the first element in the range
* [first, last) that is greater than value, or last if no such element is
* found
*
*/
private static int upperBoundNew(long[] arr, long num) {
int start = 0;
int end = arr.length - 1;
int index = 0;
int len = arr.length;
int mid = 0;
while (true) {
if (start > end) {
break;
}
mid = (start + end) / 2;
if (arr[mid] > num) {
end = mid - 1;
} else if (arr[mid] < num) {
start = mid + 1;
} else {
while (mid < len && arr[mid] == num) {
mid++;
}
if (mid == len - 1 && arr[mid] == num) {
return mid + 1;
} else {
return mid;
}
}
}
if (arr[mid] < num) {
return mid + 1;
}
return mid;
}
private static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException e) {
return null;
}
return tokenizer.nextToken();
}
public String nextLine() {
String line = null;
try {
tokenizer = null;
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public boolean hasNext() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
}
private class TrieTusar {
private class TrieNode {
Map<Character, TrieNode> children;
boolean endOfWord;
public TrieNode() {
children = new HashMap<>();
endOfWord = false;
}
}
private final TrieNode root;
public TrieTusar() {
root = new TrieNode();
}
public void test(String[] args){
TrieTusar ttr = new TrieTusar();
ttr.insert("baggers");
ttr.insert("beggars");
ttr.insert("in");
ttr.insert("the");
ttr.insert("blowed");
ttr.insert("bowled");
ttr.insert("barn");
ttr.insert("bran");
}
/**
* Iterative implementation of insert into trie
*/
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = current.children.get(ch);
if (node == null) {
node = new TrieNode();
current.children.put(ch, node);
}
current = node;
}
//mark the current nodes endOfWord as true
current.endOfWord = true;
}
/**
* Recursive implementation of insert into trie
*/
public void insertRecursive(String word) {
insertRecursive(root, word, 0);
}
private void insertRecursive(TrieNode current, String word, int index) {
if (index == word.length()) {
//if end of word is reached then mark endOfWord as true on current node
current.endOfWord = true;
return;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
//if node does not exists in map then create one and put it into map
if (node == null) {
node = new TrieNode();
current.children.put(ch, node);
}
insertRecursive(node, word, index + 1);
}
/**
* Iterative implementation of search into trie.
*/
public boolean search(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
TrieNode node = current.children.get(ch);
//if node does not exist for given char then return false
if (node == null) {
return false;
}
current = node;
}
//return true of current's endOfWord is true else return false.
return current.endOfWord;
}
/**
* Recursive implementation of search into trie.
*/
public boolean searchRecursive(String word) {
return searchRecursive(root, word, 0);
}
private boolean searchRecursive(TrieNode current, String word, int index) {
if (index == word.length()) {
//return true of current's endOfWord is true else return false.
return current.endOfWord;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
//if node does not exist for given char then return false
if (node == null) {
return false;
}
return searchRecursive(node, word, index + 1);
}
/**
* Delete word from trie.
*/
public void delete(String word) {
delete(root, word, 0);
}
/**
* Returns true if parent should delete the mapping
*/
private boolean delete(TrieNode current, String word, int index) {
if (index == word.length()) {
//when end of word is reached only delete if currrent.endOfWord is true.
if (!current.endOfWord) {
return false;
}
current.endOfWord = false;
//if current has no other mapping then return true
return current.children.size() == 0;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null) {
return false;
}
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
//if true is returned then delete the mapping of character and trienode reference from map.
if (shouldDeleteCurrentNode) {
current.children.remove(ch);
//return true if no mappings are left in the map.
return current.children.size() == 0;
}
return false;
}
}
}