# HDOJ 3613 Best Reward --- 扩展KMP算法求前（后）缀回文串

Problem Description

After an 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 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

The first line of input is 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 26 integers: v1, v2, ..., v26 (-100 ≤ vi ≤ 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 v1, the value of 'b' is v2, ..., 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




2 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 aba 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 acacac

Sample Output




1 6

题意：

#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAXN 500005
using namespace std;
int value[27];
char input[MAXN],s[MAXN],t[MAXN];// 输入，源串，逆串
int extend[MAXN],Next[MAXN];
int prefix[MAXN];// 前缀和数组
bool suffix_palindrome[MAXN],prefix_palindrome[MAXN];// 前后缀回文标记
void CalNext() {
int len1 = strlen(t);
int len2 = len2;
memset(Next,0,sizeof(Next));
Next[0] = len1;
int a = 1,p = 1;// a不能初始化为0，不然后面Next[i-a]就直接用了还没算出来的值
int j;
for(int i = 1;i < len1;i++) {
if(i < p && (Next[i-a]+i) < p) {
Next[i] = Next[i-a];
} else {
j = p - i;
j = max(0,j);
while(t[i+j] == t[j] && (i+j) < len1) j++;
Next[i] = j;
a = i,p = i + j;   // 更新a和p
}
}

}

// 扩展KMP算法求extend数组
void Ex_Kmp() {
int len1 = strlen(s);
int len2 = strlen(t);
CalNext();
memset(extend,0,sizeof(extend));
int pos = 0;
// 计算extend[0]
while(s[pos] == t[pos] && pos < len1 && pos < len2) {
pos++;
}
extend[0] = pos;
int a = 0,p = pos;
int j;
for(int i = 1;i < len1;i++) {
if(i < p && (Next[i-a]+i) < p) {
extend[i] = Next[i-a];
} else {
j = p - i;
j = max(0,j);
while(s[i+j] == t[j] && (i+j) < len1 && j < len2) j++;
extend[i] = j;
a = i,p = i + j;   // 更新a和p
}
}

}

int main()
{
int num;
scanf("%d",&num);
while(num--) {
for(int i = 0;i < 26;i++) {
scanf("%d",&value[i]);
}
scanf("%s",input);
// 求前缀和
memset(prefix,0,sizeof(prefix));
int len = strlen(input);
prefix[0] = value[input[0]-'a'];
for(int i = 1;i < len;i++) {
prefix[i] = prefix[i-1] + value[input[i]-'a'];
}
// 求后缀回文串
strcpy(s,input);
reverse(input,input+len);
strcpy(t,input);
Ex_Kmp();
for(int i = 0;i < len;i++) {
suffix_palindrome[i] = ( (extend[i]==len-i) ? true : false);
}
// 求前缀回文串
strcpy(s,input);
reverse(input,input+len);
strcpy(t,input);
Ex_Kmp();
for(int i = 0;i < len;i++) {
prefix_palindrome[i] = ( (extend[len-1-i]==1+i) ? true : false);
}
int ret = 0;
for(int i = 1;i < len;i++) {
int tmp = 0;
if(suffix_palindrome[i]) tmp += prefix[len-1] - prefix[i-1];
if(prefix_palindrome[i-1]) tmp += prefix[i-1];
ret = max(ret,tmp);
}
printf("%d\n",ret);
}
return 0;
}