Best Reward

来源:互联网 发布:flow水刀软件 编辑:程序博客网 时间:2024/06/05 15:46

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: v 1, v 2, ..., v26 (-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 v1, 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
21 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
题意:要将给出的字符串切成两段,将两段中为回文串的价值加入总价值中,输出不同切割方案中最大的总价值。(一个回文串的价值为其每个字符在给出26个数字中对应的值)。

题解:将s反置得到u。分别跑s每个位置与u的最长前缀得到ex[ ]与u每个位置与s的最长前缀得到ex1[ ]。然后枚举断点。判断断点前的一个点在ex[ ]上的值是否大于以i为终点的前缀长度的一半,若是,则将字符串价值加入总价值,否则不加入;同理判断断点之后的字符串。最后取最大值即是答案。

代码:

#include <cstdio>#include <cstdlib>#include <iostream>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <cmath>#include <ctime>using namespace std;typedef long long ll;typedef pair<int, int> P;const int INF = 0x3f3f3f3f;const ll LINF = 0x3f3f3f3f3f3f3f3f;const double PI = acos(-1.0);const double eps = 1e-10;const int maxn = 5e5+7;const int mod = 10007;int v[30],t;char s[maxn],u[maxn];int nt[maxn],ex[maxn],nt1[maxn],ex1[maxn],cost[maxn];int len;void init(){    int i = 0,po = 1;    memset(nt,0,sizeof(nt));    memset(nt1,0,sizeof(nt1));    nt[0] = len;    while(i+1<len&&s[i]==s[i+1]) i++;    nt[1] = i;    for(int i = 2;i < len;i++){        if(nt[i-po]+i<nt[po]+po) nt[i] = nt[i-po];        else {            int j = nt[po]+po-i;            if(j < 0) j = 0;            while(i+j < len&&s[j]==s[i+j]) j++;            nt[i] = j;            po = i;        }    }    i = 0,po = 1;    nt1[0] = len;    while(i+1<len&&u[i]==u[i+1]) i++;    nt1[1] = i;    for(int i = 2;i < len;i++){        if(nt1[i-po]+i<nt1[po]+po) nt1[i] = nt1[i-po];        else {            int j = nt1[po]+po-i;            if(j < 0) j = 0;            while(i+j < len&&u[j]==u[i+j]) j++;            nt1[i] = j;            po = i;        }    }}void solve(){    init();    memset(ex,0,sizeof(ex));    memset(ex1,0,sizeof(ex1));    int po = 0;    for(int i = 0;i < len;i++){        if(nt[i-po]+i < ex[po]+po) ex[i] = nt[i-po];        else {            int j = ex[po]+po-i;            if(j < 0) j = 0;            while(j+i<len&&s[j]==u[i+j]) j++;            ex[i] = j;            po = i;        }    }    po = 0;    for(int i = 0;i < len;i++){        if(nt1[i-po]+i < ex1[po]+po) ex1[i] = nt1[i-po];        else {            int j = ex1[po]+po-i;            if(j < 0) j = 0;            while(j+i<len&&u[j]==s[i+j]) j++;            ex1[i] = j;            po = i;        }    }    for(int i = 0;i < len;i++)    {        if(!i){            cost[i] = v[s[i]-'a'];        }        else cost[i] = v[s[i]-'a']+cost[i-1];    }    int ans = 0;    for(int i = 1;i < len;i++){        int tmp = 0;        if(ex[len-i]>=(i-1)/2+1){            tmp+=cost[i-1];        }        if(ex1[i]>=(len-i-1)/2+1){            tmp+=cost[len-1]-cost[i-1];        }        ans = max(ans,tmp);    }    printf("%d\n",ans);}int main(){    scanf("%d",&t);    while(t--){        for(int i = 0;i < 26;i++){            scanf("%d",&v[i]);        }        scanf("%s",s);        len = strlen(s);        for(int i = 0;i < len;i++){            u[i] = s[len-i-1];        }        solve();    }}