Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3308 Accepted Submission(s): 1350
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.
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.
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
题意:给定26个字符分别对应的价值,将字符串一分为二,如果是回文串就算入总价值,问最大价值
思路:manecher求出所有位置的回文,遍历计算沿每个位置切开的价值,取最大值
其中要切开字符串肯定是要在字符与字符之间开刀,找到了切开的位置后,根据字符串总长度计算出左右两侧串的中心位置,用manecher验证是否是回文串
预处理每个位置前的所有价值简化计算
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <stack>
#include <cstring>
#include <queue>
#include <algorithm>
#define ll long long
#define max_ 510000
#define inf 0x3f3f3f3f
#define mod 1000000007
using namespace std;
char ss[max_],s[max_*2];
int p[max_*2],tran[30],pre[max_*2];
int main(int argc, char const *argv[]) {
int t;
scanf("%d",&t);
while(t--)
{
for(int i=0;i<26;i++)
scanf("%d",&tran[i]);
scanf(" %s",ss);
int l=strlen(ss);
s[0]='$';
s[1]='#';
for(int i=0;i<l;i++)
{
s[i+i+2]=ss[i];
s[i+i+3]='#';
}
s[l+l+2]='\0';
l=strlen(s);
pre[0]=pre[1]=0;
for(int i=2;i<l;i+=2)
{
pre[i]=pre[i-2]+tran[s[i]-'a'];
pre[i+1]=pre[i];
}
int mx=0,id=0;
for(int i=1;i<=l;i++)
{
if(i<mx)
p[i]=min(p[2*id-i],mx-i);
else
p[i]=1;
while(s[i+p[i]]==s[i-p[i]])
p[i]++;
if(i+p[i]>mx)
{
mx=i+p[i];
id=i;
}
}
int maxx=-1;
for(int i=4;i<l;i+=2)
{
int left=i/2;
int right=(i+l-1)/2;
int sum=0;
if(p[left]==left)
sum+=pre[2*left-1];
if(right+p[right]==l)
sum+=(pre[l-1]-pre[2*left-1]);
maxx=maxx>sum?maxx:sum;
}
printf("%d\n",maxx );
}
return 0;
}