Skip to content

Commit 20e9877

Browse files
Merge pull request #819 from josethomas98/master
manacher algorithm
2 parents 27ae9d3 + 83092b2 commit 20e9877

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
char text[100];
5+
int min(int a, int b)
6+
{
7+
int res = a;
8+
if(b < a)
9+
res = b;
10+
return res;
11+
}
12+
13+
void findLongestPalindromicString()
14+
{
15+
int N = strlen(text);
16+
if(N == 0)
17+
return;
18+
N = 2*N + 1; //Position count
19+
int L[N]; //LPS Length Array
20+
L[0] = 0;
21+
L[1] = 1;
22+
int C = 1; //centerPosition
23+
int R = 2; //centerRightPosition
24+
int i = 0; //currentRightPosition
25+
int iMirror; //currentLeftPosition
26+
int maxLPSLength = 0;
27+
int maxLPSCenterPosition = 0;
28+
int start = -1;
29+
int end = -1;
30+
int diff = -1;
31+
32+
//Uncomment it to print LPS Length array
33+
//printf("%d %d ", L[0], L[1]);
34+
for (i = 2; i < N; i++)
35+
{
36+
//get currentLeftPosition iMirror for currentRightPosition i
37+
iMirror = 2*C-i;
38+
L[i] = 0;
39+
diff = R - i;
40+
//If currentRightPosition i is within centerRightPosition R
41+
if(diff > 0)
42+
L[i] = min(L[iMirror], diff);
43+
44+
//Attempt to expand palindrome centered at currentRightPosition i
45+
//Here for odd positions, we compare characters and
46+
//if match then increment LPS Length by ONE
47+
//If even position, we just increment LPS by ONE without
48+
//any character comparison
49+
while ( ((i + L[i]) < N && (i - L[i]) > 0) &&
50+
( ((i + L[i] + 1) % 2 == 0) ||
51+
(text[(i + L[i] + 1)/2] == text[(i - L[i] - 1)/2] )))
52+
{
53+
L[i]++;
54+
}
55+
56+
if(L[i] > maxLPSLength) // Track maxLPSLength
57+
{
58+
maxLPSLength = L[i];
59+
maxLPSCenterPosition = i;
60+
}
61+
62+
//If palindrome centered at currentRightPosition i
63+
//expand beyond centerRightPosition R,
64+
//adjust centerPosition C based on expanded palindrome.
65+
if (i + L[i] > R)
66+
{
67+
C = i;
68+
R = i + L[i];
69+
}
70+
//Uncomment it to print LPS Length array
71+
//printf("%d ", L[i]);
72+
}
73+
//printf("\n");
74+
start = (maxLPSCenterPosition - maxLPSLength)/2;
75+
end = start + maxLPSLength - 1;
76+
printf("LPS of string is %s : ", text);
77+
for(i=start; i<=end; i++)
78+
printf("%c", text[i]);
79+
printf("\n");
80+
}
81+
82+
int main(int argc, char *argv[])
83+
{
84+
85+
strcpy(text, "babcbabcbaccba");
86+
findLongestPalindromicString();
87+
88+
strcpy(text, "abaaba");
89+
findLongestPalindromicString();
90+
91+
strcpy(text, "abababa");
92+
findLongestPalindromicString();
93+
94+
strcpy(text, "abcbabcbabcba");
95+
findLongestPalindromicString();
96+
97+
strcpy(text, "forgeeksskeegfor");
98+
findLongestPalindromicString();
99+
100+
strcpy(text, "caba");
101+
findLongestPalindromicString();
102+
103+
strcpy(text, "abacdfgdcaba");
104+
findLongestPalindromicString();
105+
106+
strcpy(text, "abacdfgdcabba");
107+
findLongestPalindromicString();
108+
109+
strcpy(text, "abacdedcaba");
110+
findLongestPalindromicString();
111+
112+
return 0;
113+
}

0 commit comments

Comments
 (0)