Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4318 Accepted Submission(s): 1774
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
问题链接:HDU3613 Best Reward
问题描述:项链S是由小写字母组成的字符串,每个小写字母对应一个值,现在要将S切成两段,S1,S2。如果为回文字符串那么么其值为构成它的各个字母对应值的和,否则其值为0。问则两段子串的价值的最大值
解题思路:Manacher + 贪心 +前缀和 。先预处理,计算出前缀和,然后枚举每个分割点,分别判断前后两个串是否为回文串,按其类型加上相应的值。问题的关键在于如何判断子串是否为回文串。p[i]的含义为在添加特殊符号的字符串中s[i]向左或向右扩展的最大长度(包括字符s[i]本身),p[i]-1表示在没有添加特殊字符的符号串中以s[i]为中心的最长回文串的长度。在判断是否为回文串时,程序分了子串长度是奇数还是偶数的情况分别讨论,可以通过在纸上举例子去理解判断条件。
AC的C++程序:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=500010;
int p[2*N],v[26],sum[N];
char s[N],q[2*N];
void Manacher()
{
memset(p,0,sizeof(p));
q[0]='&';
int l=strlen(s+1);
for(int i=1;i<=l;i++)
{
q[2*i-1]='#';
q[2*i]=s[i];
}
l=l*2+1;
q[l]='#';
q[l+1]='%';
int mx=0,id=0;
for(int i=1;i<=l;i++)
{
p[i]=mx>i?min(p[2*id-i],mx-i):1;
while(q[i+p[i]]==q[i-p[i]]) p[i]++;
if(i+p[i]>mx)
{
id=i;
mx=i+p[i];
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=0;i<26;i++)
scanf("%d",&v[i]);
scanf("%s",s+1);
//预处理,计算出前缀和
int l=strlen(s+1);
sum[0]=0;
for(int i=1;i<=l;i++)
sum[i]=sum[i-1]+v[s[i]-'a'];
Manacher();//计算出p[]数组
//枚举每个分割点
int ans=-50000010;
for(int i=1;i<l;i++)
{
int temp=0;
//计算第一段的值
if(i%2==1)//如果第一段的长度为奇数
{
int mid=i/2+1;//中心位置
int l2=p[2*mid]-1;
if(i==l2)//如果此段是回文串则加上前缀和
temp+=sum[i];
}
else//长度为偶数
{
int mid=i/2+1;
int l2=p[2*mid-1];
if(l2/2==i/2)
temp+=sum[i];
}
//计算第二段的值
int len=l-i;//第二段的长度
if(len%2==1)
{
int mid=i+len/2+1;
int l2=p[2*mid]-1;
if(l2==len)//如果此段是回文串则加上前缀和
temp+=sum[l]-sum[i];
}
else
{
int mid=i+len/2+1;
int l2=p[2*mid-1];
if(l2/2==len/2)
temp+=sum[l]-sum[i];
}
if(temp>ans)
ans=temp;
}
printf("%d\n",ans);
}
return 0;
}