
Description
One of these treasures is a necklace made up of 26 different kinds of gemstones, and the length of the necklace is n. (That is to say: n gemstones are stringed together to constitute this necklace, and each of these gemstones belongs to only one of the 26 kinds.)
In accordance with the classical view, a necklace is valuable if and only if it is a palindrome - the necklace looks the same in either direction. However, the necklace we mentioned above may not a palindrome at the beginning. So the head of state decide to cut the necklace into two part, and then give both of them to General Li.
All gemstones of the same kind has the same value (may be positive or negative because of their quality - some kinds are beautiful while some others may looks just like normal stones). A necklace that is palindrom has value equal to the sum of its gemstones' value. while a necklace that is not palindrom has value zero.
Now the problem is: how to cut the given necklace so that the sum of the two necklaces's value is greatest. Output this value.
Input
For each test case, the first line is 26 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 each test case is 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
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 1 1
Sample Output
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=;
int v[],p[*N],d1[*N],d2[*N];
char s[*N],str[*N];
int n; void kp()
{
int mx=;
int id;
///for(i=n;str[i]!=0;i++)
///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
for(int i=;i<n;i++)
{
if(mx>i)
p[i]=min(p[*id-i],p[id]+id-i);
else
p[i]=;
for( ;str[i+p[i]]==str[i-p[i]];p[i]++);
if(p[i]+i>mx)
{
mx=p[i]+i;
id=i;
}
}
} void init()
{
str[]='$';
str[]='#';
for(int i=;i<n;i++)
{
str[i*+]=s[i];
str[i*+]='#';
}
n=n*+;
s[n]=;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=;i<=;i++)
scanf("%d",&v[i]);
scanf("%s",s);
n=strlen(s);
init();
kp();
d1[]=;
for(int i=;i<n;i++)
{
if(str[i]=='#') d1[i]=d1[i-];
else d1[i]=d1[i-]+v[str[i]-'a'+];
}
d2[n]=;
for(int i=n-;i>;i--)
{
if(str[i]=='#') d2[i]=d2[i+];
else d2[i]=d2[i+]+v[str[i]-'a'+];
}
int sum,tmp=-;
for(int i=;i<n-;i=i+)
{
sum=;
if(p[(i+)/]*->=i+) sum+=d1[i];
if(p[(n+i)/]*->=n-i-) sum+=d2[i+];
if(sum>tmp) tmp=sum;
}
printf("%d\n",tmp);
}
return ;
}