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.
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.
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
1 6
用Manacher预处理所有的回文串,然后枚举切割点,注意计算价格的时候要用前缀和处理一下,否则超时。
下面AC代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int const M=3e6+10;
char str[M],str1[M];
int lr[M],book[26],sum[M],val[M];
void Manacher()
{
int len=strlen(str+1);
int pos=0,mr=0;
for(int i=1; i<=len; i++)
str1[i*2-1]='*',str1[i*2]=str[i];
str1[len=len*2+1]='*';
sum[0]=0;
for(int i=1; i<=len; i++)
{
if(str1[i]!='*')
sum[i]=sum[i-1]+book[str1[i]-'a'];
else
sum[i]=sum[i-1];
if(i<mr)lr[i]=min(lr[2*pos-i],mr-i);
else lr[i]=1;
while(1<=i-lr[i]&&i+lr[i]<=len&&str1[i-lr[i]]==str1[i+lr[i]])
lr[i]++;
if(i+lr[i]>mr)
pos=i,mr=i+lr[i];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=0; i<26; i++)
scanf("%d",&book[i]);
scanf("%s",str+1);
int len=strlen(str+1);
Manacher();
int maxval=0;
for(int i=1; i<2*len+1; i++)
{
int pre=i-lr[i];
val[i]=sum[i]-sum[pre];
val[i]*=2;
if(i%2==0)
val[i]-=book[str1[i]-'a'];
}
for(int i=3; i<=2*len-1; i+=2)
{
int lmid=(i+1)/2;
int rmid=(2*len+1+i)/2;
int lans=0,rans=0;
if(lmid+lr[lmid]-1==i)
lans=val[lmid];
if(rmid-lr[rmid]+1==i)
rans=val[rmid];
maxval=max(maxval,rans+lans);
}
printf("%d\n",maxval);
}
}