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.


InputThe 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: 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.

OutputOutput 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

题意是给出26个字母的value,然后给出一个字符串,把字符串分成两段,每段不为空,对于每段的value,如果字符串是回文就是各个字符value和,如果不是则为0,求最大value和。
这里用kmp算法协助判断回文串,首先判断s的前缀,把s反转加到s后面,变换后的s本身就是一个回文串了,然后一层一层往里找回文串,这样判断的最长相同前缀和后缀一定是回文串,因为他和自己反转后相同啊(回文串从中间分开,后半段是前半段的反转)。
由于s的长度翻倍了,所以对于切割的部位,应该原s串内,即在slen内,这里sum_value记录前缀是回文串的位置的sum(value前缀和),然后把原s反转一下,接下来就是找后缀了,同样的过程,只不过切割的位置不同,如果某个后缀是回文,切割的位置是 slen - 后缀位置(函数中flag为1的情况)。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
char s[1000005];///字符串
int Next[1000005];
int sum[500001];///value前缀和
int value[26],slen;
int sum_value[500001];///记录从某位置切开后的value总和
int ans;
void getNext() {///确立Next数组
    Next[0] = -1;
    int len = slen * 2;
    int j = -1,i = 0;
    while(i < len) {
        if(j == -1 || s[i] == s[j]) Next[++ i] = ++ j;
        else j = Next[j];
    }
}
void check(int flag){
    sum[0] = 0;
    for(int i = 0;i < slen;i ++)///计算前缀和
        sum[i + 1] = sum[i] + value[s[i] - 'a'];
    reverse_copy(s,s + slen,s + slen);///把s反转添加到s后面
    getNext();///确立此时的Next数组
    int len = slen * 2;
    while(len) {
        if(len < slen) {
            int cut = len;
            if(flag) cut = slen - len;
            sum_value[cut] += sum[len];
            ans = max(ans,sum_value[cut]);
        }
        len = Next[len];
    }
}
int main() {
    int T;
    scanf("%d",&T);
    while(T --) {
        for(int i = 0;i < 26;i ++)
            scanf("%d",&value[i]);
        scanf("%s",s);///读入s
        slen = strlen(s);
        memset(sum_value,0,sizeof(sum_value));
        ans = 0;
        check(0);
        reverse(s,s + slen);///反转一下
        check(1);
        printf("%d\n",ans);
    }
}

 先计算前缀,sumval[i]就代表从i后切开的value总和,manacher算法可以求得所有的回文子串,如果回文串是前缀,便在回文串长度下标对应位置加上前缀和,如果是后缀,便在后缀的前一位对应位置加上后缀的value和,如此找寻最大值。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 500000
using namespace std;
int v[26],sum[MAX + 1];
char t[MAX * 2 + 2] = {'@','#'};
int p[MAX * 2 + 2];
int sumval[MAX + 1];
int manacher(char *s,int len) {
    int c = 2,ans = 0;
    for(int i = 0;i < len;i ++) {
        t[c ++] = s[i];
        t[c ++] = '#';
    }
    int rp = 0,rrp = 0;
    for(int i = 1;i < c;i ++) {
        p[i] = i < rrp ? min(p[rp - (i - rp)],rrp - i) : 1;
        while(t[i + p[i]] == t[i - p[i]]) {
            p[i] ++;
        }
        if(i - p[i] == 0) sumval[(i + p[i] - 2) / 2] += sum[(i + p[i] - 2) / 2];
        else if(i + p[i] == len * 2 + 2) sumval[(i - p[i]) / 2] += sum[len] - sum[(i - p[i]) / 2];
        if(rrp < i + p[i]) {
            rrp = i + p[i];
            rp = i;
        }
    }
    for(int i = 1;i < len;i ++) {
        ans = max(ans,sumval[i]);
    }
    return ans;
}
int main() {
    int t;
    char s[MAX + 1];
    scanf("%d",&t);
    while(t --) {
        for(int i = 0;i < 26;i ++) {
            scanf("%d",&v[i]);
        }
        scanf("%s",s);
        int len = strlen(s);
        for(int i = 1;i <= len;i ++) {
            sum[i] = sum[i - 1] + v[s[i - 1] - 'a'];
            sumval[i] = 0;
        }
        printf("%d\n",manacher(s,len));
    }
}