题目链接:https://vjudge.net/problem/HDU-3613
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
题目大意:
给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串(从左往右或者从右往左读,都一样),那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0。问最多能获得多少价值?
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
#define ll long long
int v[27];
const int maxn=500005;
char str1[maxn];
char str2[maxn];
int sum[maxn];
int nex[maxn];
int extend1[maxn];
int extend2[maxn];
void get_next(char str[]) ///套模板
{
int i=0;
int p;
int len=strlen(str);
while(str[i]==str[i+1]&&i+1<len)
{
i++;
}
nex[1]=i;
p=1;
for(int i=2; i<len; i++)
{
if(nex[i-p]+i<nex[p]+p)
{
nex[i]=nex[i-p];
}
else
{
int j=nex[p]+p-i;
if(j<0) j=0;
while(i+j<len&&str[j]==str[i+j])
{
j++;
}
nex[i]=j;
p=i;
}
}
}
void extendkmp(char s1[],char s2[],int extend[]) ///套模板
{
int i=0;
int p;
int len1=strlen(s1);
int len2=strlen(s2);
get_next(s2);
while(s1[i]==s2[i]&&i<len2&&i<len1)
{
i++;
}
extend[0]=i;
p=0;
for(i=1; i<len1; i++)
{
if(nex[i-p]+i<extend[p]+p)
{
extend[i]=nex[i-p];
}
else
{
int j=extend[p]+p-i;
if(j<0) j=0;
while(i+j<len1&&j<len2&&s1[j+i]==s2[j])
{
j++;
}
extend[i]=j;
p=i;
}
}
}
///extend[i]表示为以字符串S1中以i为起点的后缀字符串和模式串S2的最长公共前缀长度.
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=0; i<26; i++)
{
scanf("%d",&v[i]);
}
scanf("%s",str1);
int len=strlen(str1);
sum[0]=0;
for(int i=0; i<len; i++)
{
sum[i+1]=sum[i]+v[str1[i]-'a'];
str2[i]=str1[len-1-i]; ///str2是str1的反转
}
str2[len]='\0';
memset(nex,0,sizeof(nex));
get_next(str1);
extendkmp(str2,str1,extend1);
memset(nex,0,sizeof(nex));
get_next(str2);
extendkmp(str1,str2,extend2);
int ans=-0x3f3f3f3f;
for(int i=1;i<len;i++)
{
int tmp=0;
if(extend1[i]+i==len)
{
tmp+=sum[len-i];
}
int pos=len-i;
if(pos+extend2[pos]==len)
{
tmp+=sum[len]-sum[pos];
}
if(tmp>ans) ans=tmp;
}
printf("%d\n",ans);
}
}