码迷,mamicode.com
首页 > 其他好文 > 详细

Best Reward

时间:2017-11-05 11:28:22      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:one   重叠   include   int   const   tor   for   different   ade   

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

标签:one   重叠   include   int   const   tor   for   different   ade   

原文地址:http://www.cnblogs.com/8023spz/p/7786831.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!