Manacher algorithm for processing the longest palindrome string (ii)

Source: Internet
Author: User

In the previous article "manacher algorithm processing the longest palindrome substring (a)" explained the principle of the Manacher algorithm, and then to the algorithm, the program in the Leetcode of the longest palindrome string passed. First of all, the Manacher algorithm maintains 3 variables. An array named radius[i], which represents the radius of the palindrome string with the center bearing i, such as abcdcba, the subscript of character D is 4, then his radius[4]=3, the radius of a 0 of a is radius[0]=0, that is, the center axis is not Considered. An IDX indicates the last palindrome with an IDX as the central Axis. If a palindrome centered on I is in a palindrome centered on the Idx. The IDX is not updated, otherwise after processing radius[i], the IDX needs to be updated to I. The last RAD maintenance idx can contain the largest range of the next word Poute, in fact when i+radius[i] to reach rad the IDX needs to be updated.

Code:

 1 class Solution {2 Public:3 string longestpalindrome (string s) {4 int n=s.size (); 5 string str ( n+1, ' 0 '); 6 bool flag=1; 7 int j=0; 8 int maxidx=0;            9 for (int I=0;i<2*n+1;i++) {ten if (flag) {str[i]= ' # '; flag=false;13 }else{14 str[i]=s[j++];15 flag=true;16}17}18 vector&             lt;int> radius (2*n+1,0), int idx=0;20 int rad=1;21 for (int i=1;i<2*n+1;i++) {22 If (i>=rad) {forceextend (str,radius,idx,rad,i); maxidx= (radius[i]>radius[maxidx] I:maxidx);}else if (i<rad) {n int j=2*idx-i;27 int Idx_radius=idx-radius [idx];28 int j_radius=j-radius[j];29 if (j_radius>idx_radius) {rad Ius[i]=radius[j];31}32 else if (j_radiUs<idx_radius) {radius[i]=idx+radius[idx]-i;34}else{35 Radiu s[i]=idx+radius[idx]-i;36 int count=1;37 while ((i+radius[i]+count) <=str.size () & amp;& (i-radius[i]-count) >=0&&str[i+radius[i]+count]==str[i-radius[i]-count]) Coun                         t++;39 radius[i]+= (count-1); if (i+radius[i]>=rad) {41 idx=i;42 rad=i+count;43}44}45 maxidx= (radius [i]>radius[maxidx]?i:maxidx];}47}49 string ret=getmaxsubstring (str,maxid x,radius[maxidx]), ret;51}52 void forceextend (const string& str, vector<int>& Radi Us,int &idx,int &rad,const int k) {int count=1;54 while (k-count) >=0&& (k+count) <st R.size () &&str[k-Count]==str[k+count]) {count++;56}57 radius[k]=count-1;58 if (k+radius[k]>=rad) {5 9 idx=k;60 rad=k+count;61}62}63 string getmaxsubstring (const string &str,con             St int K,const int r) {0 string ret (r, ' '); n-j=0;66 for (int i=k-r+1;i<=k+r;i+=2) {67 ret[j++]=str[i];68}69 return ret;70}71 72};

Manacher algorithm for processing the longest palindrome string (ii)

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.