Palindrome String---best reward

Source: Internet
Author: User

HDU 3613

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 Test instructions: Given a string consisting of lowercase letters, ' a ' to ' Z ' corresponds to 26 colors of the diamond, each color of the diamond pair should have a value, will be a string into two paragraphs, to find the maximum value of the two strings, if the string is a palindrome string, this string value is the value of each diamond added, not a diamond, The value of this string is 0; Train of thought: Define two arrays d1[] and d2[],d1[i] to indicate the value of the first diamond to the I diamond and, D2[i] the value of the diamond starting from the last diamond to the left, and then from the first diamond to the last diamond traversal, If the diamond position is cut in the first place, the value of the two strings is calculated, and all diamonds are traversed once to find the maximum value; complexity: O (n);
#include <iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespacestd;Const intn=500005;intv[ -],p[2*n],d1[2*n],d2[2*N];Chars[2*n],str[2*N];intN;voidKP () {intmx=0; intID; ///For (i=n;str[i]!=0;i++)///str[i]=0;///without this sentence has a problem, can not live ural1297, such as data: Ababa ABA;     for(intI=1; i<n;i++)    {        if(mx>i) p[i]=min (p[2*id-i],p[id]+id-i); ElseP[i]=1;  for(; Str[i+p[i]]==str[i-p[i]];p [i]++); if(p[i]+i>mx) {mx=p[i]+i; ID=i; }    }}voidinit () {str[0]='$'; str[1]='#';  for(intI=0; i<n;i++) {Str[i*2+2]=S[i]; Str[i*2+3]='#'; } N=n*2+2; S[n]=0;}intMain () {intT; scanf ("%d",&T);  while(t--)    {         for(intI=1; i<= -; i++) scanf ("%d",&V[i]); scanf ("%s", s); N=strlen (s);        Init ();        KP (); d1[0]=0;  for(intI=1; i<n;i++)        {            if(str[i]=='#') d1[i]=d1[i-1]; Elsed1[i]=d1[i-1]+v[str[i]-'a'+1]; } D2[n]=0;  for(inti=n-1;i>0; i--)        {            if(str[i]=='#') d2[i]=d2[i+1]; Elsed2[i]=d2[i+1]+v[str[i]-'a'+1]; }        intsum,tmp=-99999999;  for(intI=2; i<n-2; i=i+2) {sum=0; if(P[(i+2)/2]*2-1>=i+1) sum+=D1[i]; if(p[(n+i)/2]*2-1>=n-i-1) sum+=d2[i+1]; if(sum>tmp) tmp=sum; } printf ("%d\n", TMP); }    return 0;}

Palindrome String---best reward

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.