-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmanacher_algorithms.cpp
More file actions
64 lines (55 loc) · 1.56 KB
/
manacher_algorithms.cpp
File metadata and controls
64 lines (55 loc) · 1.56 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
#define MANALEN 200100
/// Warning: Make MANALEN double of string length
/*
1. First assign string to manacher, arr[]
2. Call init
3. Call Preprocess to convert string
4. Call calc to calculate array p[]
*/
struct MANACHER {
char arr[MANALEN+10], brr[MANALEN+10];
int p[MANALEN+10];
int an, bn, mxpalin, mxcenter;
void init() {
//Assign string to arr
an = strlen ( arr );
}
//aba -> ^#a#b#a#$
void preprocess() {
int cur = 0;
brr[cur++] = '^';
FOR(i,0,an-1) {
brr[cur++] = '#';
brr[cur++] = arr[i];
}
brr[cur++] = '#';
brr[cur++] = '$';
brr[cur] = 0;
bn = cur;
}
///For each possible center, length of palindrome it can create.
///Careful, this length is without #
void calc() {
int c = 0, r = 0;
FOR(i,1,bn-2) {
int mi = c - (i-c);
p[i] = (r > i )? MIN ( r - i, p[mi] ): 0;
while ( brr[i+p[i]+1] == brr[i-p[i]-1] ) {
p[i]++;
}
if ( i + p[i] > r ) {
r = i + p[i];
c = i;
}
}
mxpalin = 0; mxcenter = 0;
///Notice we don't multiply 2 with p[i], cause more than half of them are #
FOR(i,1,bn-2) {
if ( p[i] > mxpalin ) {
mxpalin = p[i];
mxcenter = i;
}
}
///In brr[], for each center, the palindrome extends from i-p[i] to i+p[i]
}
}mana;