Best Reward HDU 3613 (palindrome sub-string manacher)

Source: Internet
Author: User

The main topic: There is a string (all made up of lowercase letters), now to divide it into two parts, if the part is a palindrome string to calculate the sum of its value, if not a palindrome then the value is 0, the maximum value can be obtained.   Analysis: First of all understand that this maximum value may be negative, such as the following: -1-1 -1 ..... AAA such a situation regardless of how the points, the string is a palindrome string, so the maximum value is 3. The algorithm using the palindrome string is the manacher algorithm, the linear complexity.   The code is as follows: =========================================================================================================== =====
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intMAXN = 1e6+7;Const intMAXM =1007;Const intOO = 1e9+7;CharSTR[MAXN];intP[MAXN], val[ -], SUM[MAXN];BOOLLEFT[MAXN], RIGHT[MAXN];/**str[] First to save the original string, after the extended string p[] p[i] Indicates how long a palindrome with I-centric (only one side of the length), sum[] sum[i) represents the total value of the first I characters and left[] left[i] indicates that a string with prefix length i is No palindrome string right[] right[i] Indicates whether a string with a suffix length of I is a palindrome string **/voidManacher (CharStr[],intN) {    intI, id=0;  for(i=2; i<n; i++)    {        if(P[id]+id >i) p[i]= Min (p[id*2-I.], p[id]+id-i); ElseP[i] =1;  while(str[i+p[i]] = = str[IP[i]]) P[i]++; if(P[id]+id < p[i]+i) ID=i; if(P[i] = =i) left[p[i]-1] =true; if(p[i]+i-1==N) Right[p[i]-1] =true; }}intMain () {intT; scanf ("%d", &T);  while(t--)    {        inti; Memset (left,false,sizeof(left)); Memset (right,false,sizeof(right)); Memset (P,false,sizeof(p));  for(i=0; i< -; i++) scanf ("%d", &Val[i]); scanf ("%s", str); intLen =strlen (str);  for(i=1; i<=len; i++) Sum[i]= sum[i-1]+val[str[i-1]-'a'];  for(I=len; i>=0; i--) {Str[i+i+2] =Str[i]; Str[i+i+1] ='#'; } str[0] ='$'; Manacher (str, Len+len+1); intAns =-Oo;  for(i=1; i<len; i++)        {            inttemp =0; if(Left[i] = =true) Temp+=Sum[i]; if(Right[len-i] = =true) Temp+ = sum[len]-Sum[i]; Ans=max (ans, temp); } printf ("%d\n", ans); }    return 0;}

Best Reward HDU 3613 (palindrome sub-string manacher)

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.