Longest echo substring (manacher Algorithm Implementation)

Source: Internet
Author: User

# 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;
 

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.