HDU3613 Best Reward

Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 549    Accepted Submission(s): 230



Problem Description
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
 

Source
 

题意:给一串字符,每个字符都有一个价值,要分成两半,求出最大价值,分成两半的字符只有是回文串时才有价值,否则价值为0。

分析:分成两串,前一串一定含有第一个字符,所有这可以想到字符串的前缀,同样,后一串肯定包含最后一个字符,所以这是后缀。我们把字符串反转过来,设原来的字符串是s1,反转过的是s2,用s1匹配s2,就相当于那串字符的前缀匹配他自己的后缀,所以这样匹配出来的就是他的最大前缀字符串,同样用s2匹配s1,他自己的后缀匹配前缀,就匹配出来了最大后缀。



#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=500010;
int val[26];
int next[MAXN],pre[MAXN],pos[MAXN],sum[MAXN];
char s1[MAXN],s2[MAXN];
void getnext(char *a,int n)
{
    int i=0,k=-1;
    next[0]=-1;
    while(i<n)
    {
        if(k==-1||a[i]==a[k])
        {
            i++;
            k++;
            next[i]=k;
        }
        else
            k=next[k];
    }
}
int KMP(char *a,char *b,int n)  //a是要匹配的串
{
    int i=0,j=0;
    while(i<n&&j<n)
    {
        if(i==-1||a[i]==b[j])
            i++,j++;
        else
            i=next[i];
    }
    return i;
}
int main()
{
    int t,n,i;
    scanf("%d",&t);
    while(t--)
    {
        for(i=0;i<26;i++)
            scanf("%d",&val[i]);
        scanf("%s",s1);
        n=strlen(s1);
        sum[0]=0;
        for(i=0;i<n;i++)
        {
            s2[i]=s1[n-1-i];
            sum[i+1]=sum[i]+val[s1[i]-'a'];
        }
        getnext(s1,n);
        int k=KMP(s1,s2,n);
        while(k)
        {
            pre[k]=n+1;
            k=next[k];
        }
        getnext(s2,n);
        k=KMP(s2,s1,n);
        while(k)
        {
            pos[k]=n+1;     //n+1仅仅是个标记
            k=next[k];
        }
        int ans=0,temp=0;
        for(i=1;i<n;i++)
        {
            if(pre[i]==n+1)
                temp+=sum[i];
            if(pos[n-i]==n+1)
                temp+=sum[n]-sum[i];
            if(temp>ans)
                ans=temp;
            temp=0;
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值