hdu-3613 best Reward (Manacher algorithm)

Source: Internet
Author: User

Best RewardTopic links

Description

After the uphill battle, General Li won a great victory. Now the head of state decide to reward him with honor and treasures for his great exploit.

One of these treasures is a necklace made up to different kinds of gemstones, and the length of the necklace is N. (Tha Say:n gemstones is stringed together to constitute the necklace, and each of the these gemstones belongs to E of the kinds.)

In accordance with the classical view, a necklace are valuable if and only if it's a palindrome-the necklace looks the s Ame in either direction. However, the necklace we mentioned above may not be a palindrome at the beginning. The head of the state decide to cut the necklace into the other part, and then give both of them to General Li.

All gemstones of the same kind have the same value (may be positive or negative because of their quality-some kinds is b Eautiful While some others could looks just like normal stones). A necklace That is palindrom have value equal to the sum of its gemstones ' value. While a necklace-is isn't palindrom has value zero.

Now, the problem is:how to cut the given necklace so, the sum of the the the and the same necklaces ' s value is greatest. Output this value.

Input

The first line of input was a single integer T (1≤t≤10)-The number of test cases. The description of these test cases follows.

For each test case, the first line is Integers:v 1, v 2, ..., v 26 ( -100≤v i≤100, 1≤i≤26), represent the value Of gemstones of each kind.

The second line of all test case was a string made up of charactor ' a ' to ' Z '. Representing the necklace. Different charactor representing Different kinds of gemstones, and the value of ' a ' is v 1, the value of ' B ' is v 2, ..., And so on. The length of the string is no more than 500000.

Output

Output a single integer:the maximum value general Li can get from the necklace.

Sample Input

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1aba1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1ACACAC

Sample Output

16 First manacher algorithm sweep again, prefix and also sweep again, the violent enumeration in front of the palindrome string midpoint, if the first bit of the palindrome string ID is not 1, then the value of the palindrome string is 0, otherwise the prefix and calculate the total value of the previous string, so the same can also be obtained after the string values, Adding up is to divide the string into two parts to get the value, and then compare with the original answer.
#include <cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespacestd;Const intMAXN =500100;CharS[MAXN];Chartxt[maxn<<1];intp[maxn<<1];intsum[maxn<<1];intLenintval =0;intN;intv[ -];intOO =0x3f3f3f3f;voidMake () {txt[0] ='$'; intpos =0;  for(inti =0; i < Len; i++) {txt[++pos] ='#'; txt[++pos] =S[i]; }txt[++pos] ='#'; N= Len *2+2; Txt[n]=' /';}int Get(intID) {if(Txt[id] >='a'&& Txt[id] <='Z') {        returnV[txt[id]-'a']; }    return 0;}voidsolve () {intMaxx =0, id;  for(inti =1; I < n; i++) {        if(I <Maxx) P[i]= Min (P[id *2-i], P[id] + ID-i); ElseP[i] =1;  while(Txt[i-p[i]] = = Txt[i +P[i]]) {            if(i + p[i] >Maxx) {Maxx= i +P[i]; ID=i; } P[i]++; }    }     for(inti =1; I < n; i++) Sum[i] = sum[i-1] +Get(i);  for(inti =1; I < n; i++) p[i]--; intAns =-oo, Tsum =0;  for(inti =2; I < n-1; i++) {Tsum=0; intL, R; intNewl, NEWR; if(P[i]) {L= i-P[i]; R= i +P[i]; }        Else{L= i-1, r = i +1; }        if(L! =1&& r! = Len *2+1) {Tsum=0; L=1; NEWL= r, newr = Len *2+1; }        Else if(L! =1&& r = = Len *2+1) {NEWL=1, NEWR =l; Tsum= Sum[r]-Sum[l]; }        Else if(L = =1&& r! = Len *2+1) {NEWL= r, newr = Len *2+1; Tsum= Sum[r]-Sum[l]; }        Else{tsum= -Oo; }        intNewmid = (newl + newr)/2; //printf ("Check%d%d%d%d%d%d\n", L, R, Newl, NEWR, Tsum, ans, newmid);        if(Newmid-p[newmid]! =1&& Newmid + p[newmid]! = Len *2+1) {ans=Max (ans, tsum); Continue; } tsum+ = (SUM[NEWR]-SUM[NEWL]); Ans=Max (ans, tsum); } printf ("%d\n", ans);}intMain () {intT; scanf ("%d", &t);  while(t--) {         for(inti =0; I < -; i++) scanf ("%d", &V[i]); scanf ("%s", s); Memset (P,0,sizeof(p)); memset (SUM,0,sizeof(sum)); Len=strlen (s);        Make ();    Solve (); }    }

hdu-3613 best Reward (Manacher algorithm)

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.