-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReady.java
601 lines (464 loc) · 14.9 KB
/
Ready.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.stream.Collectors;
public class Library {
static int d4[][] = new int[][] {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
static int d8[][] = new int[][] {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {1, -1}, {1, 1}, {-1, -1}, {-1, 1}};
static int imax = Integer.MAX_VALUE, imin = Integer.MIN_VALUE;
static long lmax = Long.MAX_VALUE, lmin = Long.MIN_VALUE;
static long umod = (long)1e9+7, cmod = 998244353;
// checks if n is prime number or not
private static boolean is_prime(long n) {
if(n < 2) return false;
if(n == 2) return true;
if(n%2 == 0) return false;
long sq = (long)Math.sqrt(n);
for(int i=3; i<=sq; i+=2)
if(n%i == 0)
return false;
return true;
}
// greatest common divisor (hcf)
private static long get_gcd(long x, long y) {
if(x == 0) return y;
return get_gcd(y%x, x);
}
// lowest common multiple (lcm)
private static long get_lcm(long x, long y) {
return (x / get_gcd(x, y)) * y;
}
// count of numbers from 1 to n-1 which are relatively prime with n
private static long get_totient(long n) {
long res = n;
for(long p=2; p*p<=n; ++p) {
if(n%p == 0) {
while(n%p == 0) n /= p;
res *= (1.0 - (1.0 / double(p)));
}}
if(n > 1) res -= (res/n);
return res;
}
// x power y mod p
private static long get_power(long x, long y, long p) {
long res = 1L;
x %= p;
while(y > 0) {
if((y&1) == 1) res = (res*x)%p;
y >>= 1;
x = (x*x)%p;
}
return res;
}
private static void get_digpow(long x, long y, int n) {
long pw = y * (long)Math.log10(x);
String fd = Math.pow(10, pw-Math.floor(pw)) * Math.pow(10, n-1) + "";
String ld = get_power(x, y, Math.pow(10, n));
}
}
class RabinKarp {
public long[] pp;
public RabinKarp(n) {
this.pp = new long[n];
pp[0] = 1;
pp[1] = 31;
for(int i=2; i<n; ++i) pp[i] = (pp[i-1]*31)%umod;
}
public long get_string_hash(String s) {
long res = 0;
for(int i=0; i<s.length(); ++i)
res = (res + (s.charAt(i)-'a'+1)*pp[i])%umod;
return res;
}
public long get_char_hash(char c, int i) {
return ((c-'a'+1)*pp[i])%umod;
}
}
class FactorSeive {
public int N;
public int[] SPF;
public List<Integer> PRIMES;
public Map<Integer, Integer> PX;
public FactorSeive(int n) {
this.N = n;
}
private List<Integer> get_factors() {
List<Integer> res = new ArrayList<>();
int i = 1;
while(i*i < N) {
if(N % i == 0) res.add(i);
++i;}
if(i-(N/i) == 1) --i;
while(i > 0) {
if(N % i == 0) res.append(N/i);
--i;}
return res;
}
private void prime_sieve() {
this.PRIMES = new ArrayList<>();
int[] p = new int[N+1];
for(int i=2; i<=N; ++i)
if(p[i] == 0)
for(int j=i*i; i<=N; j+=i)
p[j] = 1;
for(int i=2; i<=N; ++i)
if(p[i] == 0)
PRIMES.add(i);
}
// sieve to get prime factors in O(logN)
private static void prime_factor_seive() {
this.SPF = new int[N+1];
SPF[1] = 1;
for(int i=2; i<=N; ++i) SPF[i] = i;
for(int i=4; i<=N; i+=2) SPF[i] = 2;
for(int i=3; i*i<=N; ++i)
if(SPF[i] == i)
for(int j=i*i; j<=N; j+=i)
if(SPF[j] == j) SPF[j] = i;
}
// count the total number of factors of n and get P1^x1, P2^x2 for every n
private static int count_factors() {
this.PX = new HashMap<>();
int res = 1, n = N;
while(n > 1) {
int cur = SPF[n], cnt = 0;
while(n > 1 && SPF[n] == cur) {
++cnt;
n /= cur;
}
res *= (cnt+1);
PX.put(cur, PX.getOrDefault(cur,0) + cnt);
}
return res;
}
}
class StringMatching {
public char[] S;
public int[] PI;
public int lenP, lenT, lenS;
public StringMatching(String pattern, String haystack) {
this.lenP = pattern.length();
this.lenT = haystack.length();
this.lenS = lenP + lenT + 1;
this.PI = new int[lenS];
this.S = (pattern + "#" + haystack).toCharArray();
}
private static List<Integer> get_kmp() {
for(int i=1; i < lenS; ++i) {
int j = PI[i-1];
while(j > 0 && S[i] != S[j]) j = PI[j-1];
if(S[i] == S[j]) ++j;
PI[i] = j;
}
List<Integer> res = new ArrayList<>();
for(int i=lenP+1; i < lenS; ++i)
if(PI[i] == lenP) res.add(i-2*lenP);
return res;
}
private static List<Integer> get_zalgo() {
int L = 0, R = 0;
for(int i=1; i < lenS; ++i) {
if(i <= R && PI[i-L] < R-i+1)
PI[i] = PI[i-L];
else {
if(i > R) L = R = i;
else L = i;
while(R < lenS && S[R-L] == S[R]) ++R;
PI[i] = (R--)-L;
}}
List<Integer> res = new ArrayList<>();
for(int i=0; i < lenS; ++i)
if(PI[i] == lenP && i-lenP-1 <= lenS-lenP)
res.add(i-lenP-1);
return res;
}
}
class FenvickTree {
public int[] FEN;
public int N;
public FenvickTree(int n) {
this.N = n+5;
this.FEN = new int[N];
}
public void add(int ind, int val) {
while(ind <= N) {
FEN[ind] += val;
ind += (ind & -ind);
}
}
public int find(int ind) {
int res = 0;
while(ind > 0) {
res += FEN[ind];
ind -= (ind & -ind);
}
return res;
}
public int find(int left, int right) {
return find(right) - find(left-1);
}
}
class DSU {
public int N;
public int[] SIZE, PARENT;
public DSU(int n) {
this.N = n;
this.SIZE = new int[n];
this.PARENT = new int[n];
for(int i=0; i < n; ++i) {
SIZE[i] = 1;
PARENT[i] = i;}
}
public int find(int x) {
if(PARENT[x] != x)
PARENT[x] = find(PARENT[x]);
return PARENT[x];
}
public void merge(int x, int y) {
int a = PARENT[x], b = PARENT[y];
if(a == b) return;
if(SIZE[a] < SIZE[b]) {
a = a^b; b = a^b; a = a^b;}
PARENT[b] = a;
SIZE[a] += SIZE[b];
}
}
class TrieNode {
public int ALPHABET_SIZE = 26;
public TrieNode[] CHILDREN;
public boolean IS_END;
public TrieNode() {
this.CHILDREN = new TrieNode[ALPHABET_SIZE];
this.IS_END = false;
}
}
class Trie {
public TrieNode ROOT;
public Trie() {
this.ROOT = new TrieNode();
}
public void insert(String s) {
TrieNode pCrawl = this.ROOT;
for(int i=0; i < s.length(); ++i) {
int idx = s.charAt(i)-'a';
if(pCrawl.CHILDREN[idx] == null) pCrawl.CHILDREN[idx] = new TrieNode();
pCrawl = pCrawl.CHILDREN[idx];
}
pCrawl.IS_END = true;
}
public boolean search(String s) {
TrieNode pCrawl = this.ROOT;
for(int i=0; i < s.length(); ++i) {
int idx = s.charAt(i)-'a';
if(pCrawl.CHILDREN[idx] == null) return false;
pCrawl = pCrawl.CHILDREN[idx];
}
return pCrawl.IS_END;
}
}
class SegmentTree {
public int[] TREE;
public int N;
public SegmentTree(int[] a) {
this.N = a.length+5;
this.TREE = new int[2*N];
for(int i=0; i<N; ++i) TREE[N+i] = a[i];
for(int i=N-1; i > 0; --i) TREE[i] = TREE[i<<1] + TREE[i<<1 | 1];
}
public void update(int p, int val) {
for(TREE[p+=N] = val; p > 1; p>>=1) TREE[p>>1] = TREE[p] + TREE[p^1];
}
public int query(int l, int r) {
int res = 0;
for(l+=N, r+=N; l < r; l>>=1, r>>=1) {
if((l&1) > 0) res += TREE[l++];
if((r&1) > 0) res += TREE[--r];
}
return res;
}
}
class LazySegmentTree {
public int[] TREE;
public int[] LAZY;
public int N, H;
public SegmentTree(int[] a) {
this.N = a.length+5;
this.H = Integer.SIZE - Integer.numberOfLeadingZeroes(N);
this.TREE = new int[2*N];
this.LAZY = new int[2*N];
for(int i=0; i<N; ++i) TREE[N+i] = a[i];
for(int i=N-1; i > 0; --i) TREE[i] = TREE[i<<1] + TREE[i<<1 | 1];
}
public void apply(int p, int val) {
TREE[p] += val;
if(p < N) LAZY[p] += val;
}
public void build(int p) {
while(p > 1) {
p>>=1;
TREE[p] = Math.max(TREE[p<<1], TREE[p<<1 | 1]) + LAZY[p];
}
}
public void push(int p) {
for(int s = H; s > 0; --s) {
int i = p >> s;
if(LAZY[i] != 0) {
apply(i<<1, LAZY[i]);
apply(i<<1 | 1, LAZY[i]);
LAZY[i] = 0;
}
}
}
public void update(int l, int r, int val) {
l+=N; r+=N;
int l0 = l, r0 = r;
for(; l < r; l>>=1, r>>=1) {
if((l&1) > 0) apply(l++, val);
if((r&1) > 0) apply(--r, val);
}
build(l0); build(r0-1);
}
public int query(int l, int r) {
l+=N; r+=N;
push(l); push(r-1);
int res = -(int)2e9;
for(; l < r; l>>=1, r>>=1) {
if((l&1) > 0) res = Math.max(res, TREE[l++]);
if((r&1) > 0) res = Math.max(TREE[--r], res);
}
return res;
}
}
class Aho {
public Aho[] CHILDREN;
public int PATTERN_INDEX;
public Aho SUFFIX_LINK, OUTPUT_LINK;
public Aho() {
CHILDREN = new Aho[26];
SUFFIX_LINK = null;
OUTPUT_LINK = null;
PATTERN_INDEX = Integer.MAX_VALUE;
}
}
class Corasick {
public Aho ROOT;
public Corasick() {
this.ROOT = new Aho();
}
public void insert(String s, int ind) {
Aho pCrawl = this.ROOT;
for(int i=0; i < s.length(); ++i) {
int idx = s.charAt(i)-'a';
if(pCrawl.CHILDREN[idx] == null)
pCrawl.CHILDREN[idx] = new Aho();
pCrawl = pCrawl.CHILDREN[idx];
}
pCrawl.PATTERN_INDEX = Math.min(pCrawl.PATTERN_INDEX, ind);
}
public boolean not_root(Aho pCrawl) {
if(pCrawl == null || pCrawl == this.ROOT)
return false;
return true;
}
public void set_links() {
this.ROOT.SUFFIX_LINK = this.ROOT;
Aho temp, pCrawl;
Queue<Aho> q = new LinkedList<>();
q.offer(this.ROOT);
while(!q.isEmpty()) {
pCrawl = q.poll();
for(int i=0; i<26; ++i) {
if(not_root(pCrawl.CHILDREN[i])) {
q.offer(pCrawl.CHILDREN[i]);
temp = pCrawl.SUFFIX_LINK;
while(not_root(temp) && !not_root(temp.CHILDREN[i]))
temp = temp.SUFFIX_LINK;
if(not_root(pCrawl.CHILDREN[i]) && temp != pCrawl) {
pCrawl.CHILDREN[i].SUFFIX_LINK = temp.CHILDREN[i];
// pCrawl.CHILDREN[i].PATTERN_INDEX = Math.min(pCrawl.CHILDREN[i].PATTERN_INDEX, temp.CHILDREN[i].PATTERN_INDEX);
}
else pCrawl.CHILDREN[i].SUFFIX_LINK = this.ROOT;
}
}
if(pCrawl.SUFFIX_LINK.PATTERN_INDEX < Integer.MAX_VALUE)
pCrawl.OUTPUT_LINK = pCrawl.SUFFIX_LINK;
else pCrawl.OUTPUT_LINK = pCrawl.SUFFIX_LINK.OUTPUT_LINK;
}
}
public boolean search(String s, int ind) {
Aho pCrawl = this.ROOT;
for(int i=0; i < s.length(); ++i) {
int idx = s.charAt(i)-'a';
if(not_root(pCrawl.CHILDREN[idx])) {
pCrawl = pCrawl.CHILDREN[idx];
if(pCrawl.PATTERN_INDEX < ind)
return true;
Aho mol = pCrawl.OUTPUT_LINK;
while(not_root(mol)) {
if(mol.PATTERN_INDEX < index)
return true;
mol = mol.OUTPUT_LINK;
}
}
else {
while(pCrawl != this.ROOT && pCrawl.CHILDREN[idx] == null)
pCrawl = pCrawl.SUFFIX_LINK;
if(pCrawl.CHILDREN[idx] != null) --i;
}
}
return false;
}
}
class SUffixArray {
public int N;
public String S;
public int MAXN = 100010;
public int[] LCP, RA, SA, TRA, TSA;
public SUffixArray(String s) {
this.S = s+"$";
this.N = s.length();
this.RA = new int[N]; this.TRA = new int[N];
this.SA = new int[N]; this.TSA = new int[N];
}
public void cSort(int k) {
int maxi = Math.max(300, N), sum = 0;
int[] c = new int[MAXN];
for(int i=0; i < N; ++i) ++c[(i+k < N ? RA[i+k] : 0)];
for(int i=0; i < maxi; ++i) {
int tp = c[i];
c[i] = sum;
sum += tp;
}
for(int i=0; i < N; ++i)
TSA[c[(SA[i]+k > 0 ? RA[SA[i]+k] : 0)]++] = SA[i];
for(int i=0; i < N; ++i) SA[i] = TSA[i];
}
public void build() {
for(int k=1; k < N; k*=2) {
cSort(k); cSort(0);
int r = 0;
TRA[SA[0]] = r;
for(int i=1; i < N; ++i) {
if(RA[SA[i]] == RA[SA[i-1]] && RA[SA[i]+k] == RA[SA[i-1]+k]) TRA[SA[i]] = r;
else TRA[SA[i]] = ++r;
}
for(int i=0; i < N; ++i) RA[i] = TRA[i];
if(RA[SA[N-1]] == N-1) break;
}
}
public void kasai() {
int k = 0;
this.LCP = new int[N];
int[] rank = new int[N];
for(int i=0; i < N; ++i) rank[SA[i]] = i;
for(int i=0; i < N; ++i) {
if(rank[i] == N-1) {
k = 0;
continue; }
int j = SA[rank[i]+1];
while(i+k < N && j+k < N && S.charAt(i+k) == S.charAt(j+k))
++k;
LCP[rank[i]] = k;
}
}
}