# Include <iostream>
Using namespace std;
# Define M 1000
Void Manacher (char * str, char * maxpalindrome) // str accepts the maximum length of the string stored in maxpalindrome
{
Int I, j;
Int maxrad = 0, position = 0; // record the longest retrieval radius and the location where the longest retrieval radius appears
Int mx = 0, id; // id indicates the character position at the longest extension position, and the farthest position from the right extension.
Char temp [2 * M + 2]; // used to store processed strings
Int rad [M]; // array of return radius
Int length = strlen (str );
Int num = 2 * strlen (str) + 2; // record the string length after inserting characters
// Process the original string
Temp [0] = '$ ';
For (I = 0; I <= length; I ++)
{
Temp [2 * I + 1] = '#';
Temp [2 * I + 2] = str [I];
}
Cout <temp <endl;
// Calculate the input return radius Array
For (I = 1; I <num; I ++)
{
If (mx> I)
Rad [I] = rad [2 * id-I] <(mx-I )? Rad [2 * id-I] :( mx-I );
Else
Rad [I] = 1; // The return radius must be at least 1
For (; temp [I + rad [I] = temp [I-rad [I]; rad [I] ++); // find the maximum retrieval radius
If (rad [I] + I> mx) // refresh the maximum extended position to the right of the input string whose position is greater than that of the previous character
{
MX = rad [I] + I-1; // record the new extended position where the extended position starts from 1
Id = I; // the location where the Longest Extended character appears.
}
}
For (I = 1; I <num; I ++) // find the maximum retrieval radius and the place where the maximum retrieval radius appears.
{
If (RAD [I]> maxrad)
{
Maxrad = rad [I]; // record the maximum retrieval radius
Position = I; // The place where the record shows the maximum return radius
}
}
// Copy the longest return substring according to the return radius and the location with the maximum return radius
For (I = 0, j = position-maxrad + 1; j <Position + maxrad-1; j ++)
{
If (temp [j]! = '#' & Temp [j]! = '$ ')
{
Maxpalindrome [I] = temp [j];
I ++;
}
}
Maxpalindrome [I] = '\ 0 ';
}
Int main ()
{
Char str [] = "gdabebajuk ";
Char maxpalindrome [M];
Manacher (str, maxpalindrome );
Cout <maxpalindrome <endl;
Return 0;
}