-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManacher_Algorithm.cpp
More file actions
174 lines (136 loc) · 4.2 KB
/
Copy pathManacher_Algorithm.cpp
File metadata and controls
174 lines (136 loc) · 4.2 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
// Given a string, find the longest substring which is palindrome.
// if the given string is “forgeeksskeegfor”, the output should be “geeksskeeg”
// A C++ program to implement Manacher’s Algorithm
#include <bits/stdc++.h>
using namespace std;
void findLongestPalindromicString(string text)
{
int N = text.length();
if (N == 0)
return;
// Position count
N = 2 * N + 1;
// LPS Length Array
int L[N];
L[0] = 0;
L[1] = 1;
// centerPosition
int C = 1;
// centerRightPosition
int R = 2;
// currentRightPosition
int i = 0;
// currentLeftPosition
int iMirror;
int expand = -1;
int diff = -1;
int maxLPSLength = 0;
int maxLPSCenterPosition = 0;
int start = -1;
int end = -1;
// Uncomment it to print LPS Length array
// printf("%d %d ", L[0], L[1]);
for (i = 2; i < N; i++)
{
// Get currentLeftPosition iMirror
// for currentRightPosition i
iMirror = 2 * C - i;
// Reset expand - means no
// expansion required
expand = 0;
diff = R - i;
// If currentRightPosition i is
// within centerRightPosition R
if (diff >= 0)
{
// Case 1
if (L[iMirror] < diff)
L[i] = L[iMirror];
// Case 2
else if (L[iMirror] == diff && R == N - 1)
L[i] = L[iMirror];
// Case 3
else if (L[iMirror] == diff && R < N - 1)
{
L[i] = L[iMirror];
// Expansion required
expand = 1;
}
// Case 4
else if (L[iMirror] > diff)
{
L[i] = diff;
// Expansion required
expand = 1;
}
}
else
{
L[i] = 0;
// Expansion required
expand = 1;
}
if (expand == 1)
{
// Attempt to expand palindrome centered
// at currentRightPosition i. Here for odd
// positions, we compare characters and
// if match then increment LPS Length by ONE
// If even position, we just increment LPS
// by ONE without any character comparison
while (((i + L[i]) < N && (i - L[i]) > 0) && (((i + L[i] + 1) % 2 == 0) || (text[(i + L[i] + 1) / 2] == text[(i - L[i] - 1) / 2])))
{
L[i]++;
}
}
// Track maxLPSLength
if (L[i] > maxLPSLength)
{
maxLPSLength = L[i];
maxLPSCenterPosition = i;
}
// If palindrome centered at
// currentRightPosition i expand
// beyond centerRightPosition R,
// adjust centerPosition C based
// on expanded palindrome.
if (i + L[i] > R)
{
C = i;
R = i + L[i];
}
// Uncomment it to print LPS Length array
// System.out.print("%d ", L[i]);
}
start = (maxLPSCenterPosition - maxLPSLength) / 2;
end = start + maxLPSLength - 1;
// System.out.print("start: %d end: %d\n",
// start, end);
cout << "LPS of string is " << text << " : ";
for (i = start; i <= end; i++)
cout << text[i];
cout << endl;
}
int main()
{
string text1 = "babcbabcbaccba";
findLongestPalindromicString(text1);
string text2 = "abaaba";
findLongestPalindromicString(text2);
string text3 = "abababa";
findLongestPalindromicString(text3);
string text4 = "abcbabcbabcba";
findLongestPalindromicString(text4);
string text5 = "forgeeksskeegfor";
findLongestPalindromicString(text5);
string text6 = "caba";
findLongestPalindromicString(text6);
string text7 = "abacdfgdcaba";
findLongestPalindromicString(text7);
string text8 = "abacdfgdcabba";
findLongestPalindromicString(text8);
string text9 = "abacdedcaba";
findLongestPalindromicString(text9);
return 0;
}
// This code is contributed by Ishankhandelwals.