Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613
Test instructions is to give you a string s and then seek to divide s into two parts after the sum of the value of how much, separate string if is a palindrome so value is the sum of the value of each letter, if not so value is 0;
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Abbadf
Then the value that can be divided into ABBA and DF Abba is 1+2+2+1=6,DF not palindrome, so the value is 0; The total value is 6;
We can use the array L "I" R "I" to record whether the prefix length of the string is a palindrome string and suffix length i is a palindrome string;
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intN = 1e6+7;intv[ -], Sum[n], p[n];///Sum[i] Represents the sum of the values of the first I characters in the s string;///P[i] Represents the radius of the palindrome string centered on I (including I itself);CharS[n];BOOLL[n], r[n];///L[i] Indicates whether the first I character is a palindrome string, R[i] Indicates whether the suffix of length i is a palindrome string;voidManacher (CharS[],intN) { intId =0, mx =0; for(intI=2; i<n; i++) { if(mx>i) p[i]= Min (mx-i, p[id*2-i]); ElseP[i]=1; while(S[i+p[i]] = = s[i-P[i]]) P[i]++; if(MX < p[i]+i) {mx= p[i]+i; Id=i; } if(P[i] = =i) l[p[i]-1] =true; if(P[i]+i = =N) r[p[i]-1] =true; }}intMain () {intT; scanf ("%d", &T); while(t--) {memset (L,0,sizeof(L)); memset (R,0,sizeof(R)); Memset (P,0,sizeof(p)); memset (SUM,0,sizeof(sum)); for(intI=0; i< -; i++) scanf ("%d", &V[i]); scanf ("%s", s); intLen =strlen (s); for(intI=1; i<=len; i++) Sum[i]+ = sum[i-1] + v[s[i-1]-'a']; for(intI=len; i>=0; i--) {s[i+i+2] =S[i]; S[i+i+1] ='#'; } s[0]='$'; Manacher (s),2*len+2); intAns =0; for(intI=1; i<len; i++) { intt=0; if(L[i]) T+=Sum[i]; if(r[len-i]) T+=sum[len]-Sum[i]; Ans=max (ans, t); } printf ("%d\n", ans); } return 0;}View Code
Best Reward---hdu3613 (manacher palindrome)