1. 程式人生 > >Best Reward

Best Reward

nta others line || ans copy res exploit ever

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和,如果不是則為0,求最大的sumval。


代碼:


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

using namespace std;
char s[1000005];//s是字符串,之所以是最大大小的兩倍是因為思路用到了回文的特性,反轉後相等,借助kmp,在s後面加上原串的反轉,可以找出回文串
int nexti[1000005],sum[500001];///nexti記錄長度為i的字符串中前綴和後綴重疊的最大大小,sum記錄前綴和
int val[26],n,sumval[500001] = {0};//val記錄26個字母的value,sumval記錄某個位置劃分的前綴value
void getnexti()//確立nexti
{
    nexti[0] = -1;
    int k = -1,i = 0;
    while(i < (n << 1))
    {
        if(k == -1 || s[i] == s[k])nexti[++ i] = ++ k;
        else k = nexti[k];
    }
}
void check(int k)
{
    sum[0] = 0;
    for(int i = 0;i < n;i ++)
        sum[i + 1] = sum[i] + val[s[i] - a];
    reverse_copy(s,s+n,s+n);///反轉添加到s後,然後kmp
    getnexti();
    int p = n << 1;
    if(k == 0)///原串求前綴
    {
        while(p != 0)
        {
            if(p < n)sumval[p] += sum[p];
            p = nexti[p];
        }
    }
    else///原串進行反轉,相當於求後綴,把同一位置的前綴和後綴和加到一塊就好了,然後求最大值
    {
        int ans = 0;
        while(p != 0)
        {
            if(p < n)
            {
                sumval[n - p] += sum[p];
                ans = max(ans,sumval[n - p]);
            }
            p = nexti[p];
        }
        cout<<ans<<endl;
    }
}
int main()
{
    int T;
    cin>>T;
    while(T --)
    {
        for(int i = 0;i < 26;i ++)
            cin>>val[i];
        cin>>s;
        n = strlen(s);
        memset(sumval,0,sizeof(int)*n);///之前一直wa,因為這裏沒有清0
        check(0);
        reverse(s,s+n);
        check(1);
    }
}

Best Reward