Best Reward

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值