Fortune left France advanced classes 1_4Manacher algorithm

. 1 #include <the iostream>
 2 #include < String >
 . 3  
. 4  the using  namespace STD;
 . 5  
. 6  // use manacher algorithms for the characters in the longest substring palindromic 
. 7  
. 8  int Manacher ( String STR)
 . 9  {
 10      // string pre processing 
. 11      String newStr = " # " ;
 12 is      for (Auto C: STR)
 13 is          newStr newStr = C + + " # " ;
 14      // palindromic radial array of records 
15      intRindex = * new new  int [newStr.length ()];
 16      int C = - . 1 ; // palindromic center 
. 17      int R = - . 1 ; // palindromic right border R palindrome located to the right of the right boundary of a letter 
18      int maxL = INT_MIN; // record the longest word length back 
. 19      for ( int I = 0 ; I <newStr.length (); ++ I)
 20 is      {
 21 is          // direct access to the shortest possible radius of the first step palindromic when i> R, the minimum radius is a palindrome,
 22          // contrary, the shortest radius may be palindromic corresponding i i 'i palindromic radius or distance R to 
23 is          iF (R> i)
 24             rindex [I] = rindex [ 2 ? * C - I] <R & lt - I rindex [ 2 * C - I]: R & lt - I;
 25          the else 
26 is              rindex [I] = . 1 ;
 27          // after the start from a minimum value violence boundary matching, the match fails to exit 
28          the while (I + rindex [I] <newStr.length () && I - rindex [I]> - . 1 )
 29          {
 30              IF (newStr [I + rindex [I]] = newStr = [I - rindex [I]]) // to the side extension, back alphabet 
31 is                  rindex [I] ++ ;
 32              the else 
33 is                  BREAK ;
 34 is          }
 35          //When R C is observed and can be updated 
36          IF (I + rindex [I]> R)
 37 [          {
 38 is              R = I + rindex [I];
 39              C = I;
 40          }
 41 is          // updates the maximum radius value palindrome 
42 is          maxL = maxL> rindex [I]? maxL: rindex [I];
 43 is      }
 44 is      Delete [] rindex;
 45      // explain why the return value is here maxn-1, and because of different lengths of the original string manacherstring,
 46      // so obtained here it is the greatest radius of the maximum palindrome palindrome substring of the original string length plus 1,
 47      // are interested can try their verification, in order to later -1 '#' is removed 
48      return maxL - 1;    
49     
50 }
51 
52 void Test()
53 {
54     string str = "abc1234321ab";
55     cout << Manacher(str) << endl;
56 }

 

Reproduced in: https: //www.cnblogs.com/zzw1024/p/11031706.html

Guess you like

Origin blog.csdn.net/weixin_34072637/article/details/93252290