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. 

题意:给出一个由26种字母组成的字符串,每种字母有权值,当一个串是回文串的时候,它的权值是其中所有字母的权值之和,否则是0。

现在要将这个字符串切成两段,问两段的权值和最大是多少。

用前缀和处理串的前缀权值和。

manacher进行回文串的匹配处理。

贪心切分位置并用manacher处理的数组判断是否是回文子串,并用前缀和数组求值,取最大值。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=5e5+5;
 7 char s[maxn],t[maxn<<1];
 8 int p[maxn<<1];
 9 int a[maxn],v[26];
10 
11 void manacher(){
12     int len=strlen(s),l=0;
13     t[l++]='$';
14     t[l++]='#';
15     for(int i=0;i<len;++i){
16         t[l++]=s[i];
17         t[l++]='#';
18     }
19     t[l]=0;
20     int maxx=0,num=0;
21     for(int i=0;i<l;++i){
22         p[i]=maxx>i?min(p[2*num-i],maxx-i):1;
23         while(t[i+p[i]]==t[i-p[i]])p[i]++;
24         if(i+p[i]>maxx){
25             maxx=i+p[i];
26             num=i;
27         }
28     }
29 }
30 
31 int main(){
32     int T;
33     scanf("%d",&T);
34     while(T--){
35         for(int i=0;i<26;++i)scanf("%d",&v[i]);
36         scanf("%s",s);
37         int l=strlen(s);
38         a[0]=v[s[0]-'a'];
39         for(int i=1;i<l;++i){
40             a[i]=a[i-1]+v[s[i]-'a'];
41         }
42         manacher();
43         int ans=0;
44         for(int i=0;i<l-1;++i){
45             int tmp=0;
46             int num=p[i+2]-1;
47             if(num==i+1)tmp+=a[i];
48             num=p[i+l+2]-1;
49             if(num==l-i-1)tmp+=a[l-1]-a[i];
50             if(tmp>ans)ans=tmp;
51         }
52         printf("%d\n",ans);
53     }
54     return 0;
55 }
View Code