acwing 139. The maximum length of palindromic substring

Questions surface:

If you read a string of positive, forward and backwards reading is the same, it is called a palindrome.

Given a string of length N, S, ask him length of the longest substring palindrome is.

Input Format

Input will contain a maximum of 30 test cases, each test case per line, in order to form up to 1 million lowercase characters are given.

To input a character string "END" (without the quotes) represents the beginning of input line termination.

Output Format

For each test case in the input, output and the maximum number of test cases palindrome substring lengths (refer to sample format).

Each output per line.

Sample input:

abcbabcbabcba
abacacbaaaab
END

Sample output:

Case 1: 13
Case 2: 6
Solution:
horse-drawn vehicles template topic, this topic with complex partial character hash:

Manacher algorithm, but we question head hash is to exercise our level, so we just say here how to use a hashing algorithm to solve the authors forget how to use a horse-drawn car ... algorithm
on a bunny rabbit rabbit problem, we know that two judges whether strings are equal, you can use the hash string, the string is counted as P hexadecimal values, then you can determine the interval and then This question we need a positive string, but also the character of an anti string, then the string if the string is equal to the positive opposite, then the odd palindrome string to 2 + 1, even-numbered palindrome string directly 2 can be. the reason to do this, because we are right to be a palindrome, we need the palindrome dismantling become a positive and a string of anti-string, so we go deal with this question head.
That being the case, we can calculate a prefix and then calculates a suffix and then you know, and a string of positive anti-string string hash value is a hash value and the range.
after the count finished, we are currently only need to enumerate a mid middle point, because all palindromes are strings have an intermediate point (odd), or Room interval (even), and each half of the length of the string to find, remember not palindromic sequence palindromic string length, string length * 2 + 1 (odd) or a string length * 2 (even ).
Remember that if the maximum palindrome string of 1 (that is, all the characters are not the same, for example abcdefg), then the output is 1, not 3, odd-odd string palindrome string = * 2 + 1, you have to be careful Japanese sentence this case, the processing or binary boundary.

Author: Qinhuai shore lights dim
link: https: //www.acwing.com/solution/acwing/content/906/
Source: AcWing
copyright reserved by the authors. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source. (Solution to a problem handling)

Code

. 1 #include <the iostream>
 2 #include <algorithm>
 . 3 #include <CString>
 . 4 #include <the cmath>
 . 5  the using  namespace STD;
 . 6  #define ULL unsigned Long Long
 . 7 ULL P [ 2.00001 million ], HL [ 2.00001 million ], HR [ 2.00001 million ]; // HL memory array from left to right, hr vice versa 
. 8  char STR [ 2.00001 million ];
 . 9  const  int  Base = 131 is ;
 10 ULL GET (ULL H [], int L, int R & lt)
11 {
12     return h[r]-h[l-1]*p[r-l+1];
13 }
14 int main()
15 {
16     int t=1;
17     while(scanf("%s",str+1))
18     {
19         if (strcmp(str+1,"END")==0) //结束读入
20             return 0;
21     p[0]=1;
22     int len=strlen(str+1);
23     for(int i=len*2;i>0;i-=2)
24     {
25         str[i]=str[i/2];
26         str[i-1]='z'+1;
27     }
28     len*=2;
29     str[++len]='z'+1;
30     for(int i=1,j=len;i<=len;i++,j--)
31     {
32         hl[i]=hl[i-1]*base+str[i]-'a'+1;
33         hr[i]=hr[i-1]*base+str[j]-'a'+1;
34         p[i]=p[i-1]*base;
35     }
36     
37      int res=0;
38      for(int i=1;i<=len;i++)
39      {
40          int l=0,r=min(i-1,len-i);
41          while(l<r)//二分回文长度
42          {
43              int mid=(l + r + 1) >> 1;
44              if(get(hl,i-mid,i-1)!=get(hr,len-(i+mid)+1,len-(i+1)+1))r=mid-1;//
45              else
46              l=mid;
47          }
 48           IF (STR [IL] <= ' Z ' ) RES = max (RES, L + . 1 ); // If the last letter of the (2 * l + 1) rounded up
 49           the else 
50           RES = max ( RES, L);
 51 is       }
 52 is       the printf ( " Case% D: D% \ n- " , T ++ , RES);
 53 is      }
 54 is      return  0 ;
 55 }

Guess you like

Origin www.cnblogs.com/flyljz/p/11648989.html