您的位置:首页 > 其它

HDU3613 Best Reward 3连发之manacher

2016-03-22 20:56 246 查看
题目链接:HDU3613


Best Reward

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1420 Accepted Submission(s): 576



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: v1, v2, ..., v26 (-100 ≤ vi ≤ 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 v2, ..., 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

2010 ACM-ICPC Multi-University
Training Contest(18)——Host by TJU

Recommend

lcy | We have carefully selected several similar problems for you: 3618 3620 3614 3615 3616

题目分析:这次采用manacher求解,由于manacher本身就是用来求解回文串问题,所以看起来比较直观。直接模板求串中以每个元素为中心的回文串长度,然后每求一个判断回文串是是否在前缀或者后缀,是的话标记一下,接下来求最大值就行了。

之前忘了说,,注意要预处理下所有可能的前缀值的和否则可能会超时。第二要注意必须要分一次,不可以不分。比如串长度为1就可以直接输出0了。

//
//  main.cpp
//  HDU3613a
//
//  Created by teddywang on 16/3/10.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
int v[27];
char s[1000005];
int n,p[1000005],sum[500005],per[500005],pos[500005];
int main()
{
int len;
cin>>n;
while(n--)
{
for(int i=0;i<26;i++)
scanf("%d",&v[i]);
scanf("%s",s);
len=strlen(s);
if(len==1)
{
cout<<0<<endl;
continue;
}
for(int i=1;i<=len;i++)
sum[i]=sum[i-1]+v[s[i-1]-'a'];
for(int i=len;i>=0;i--)
{
s[2*i+2]=s[i];
s[2*i+1]='#';
}
s[0]='*';
int maxlen=0,id=0;
for(int i=2;i<2*len+1;i++)
{
maxlen=id+p[id]-1;
if(p[id]+id>i) p[i]=min(p[2*id-i],maxlen-i+1);
else p[i]=1;
while(s[p[i]+i]==s[i-p[i]]) p[i]++;
if(p[id]+id<p[i]+i) id=i;
if(p[i]==i) per[p[i]-1]=n+1;
if(p[i]+i==2*len+2) pos[p[i]-1]=n+1;
}
int maxl=0,ans=-inf;
for(int i=1;i<len;i++)
{
maxl=0;
if(per[i]==n+1) maxl+=sum[i];
if(pos[len-i]==n+1) maxl+=sum[len]-sum[i];
if(ans<maxl) ans=maxl;
if(ans==sum[len]) break;
}
cout<<ans<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: