Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2739 Accepted Submission(s): 1117
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
此题使用的为扩展kmp的运用,托的的时间有点长,可能是智商低把
,第一天在别人博客看到这道题,准备去做,但扩展kmp不怎么熟练,就耽搁了一天,今天就来发一波自己对此题的理解,(虽然很水
)权当是做题纪录了。
这道题的意思是,给你一个t组测试数据,然后给你26个单词的价值,再给你一个字符串,让你将该字符串切成两份,切成回文串的可以计算其价值,因此要使切出来的价值最大,理想状态下是两个都是回文串,那么所有的情况要提前知道,切割分两部分,第一部分标 1 第二部分 2,y是回文, n不是回文。
因此有四种情况,
分别是
1y 2y ;
1y 2n;
1n 2y;
1n 2n;
没错!情况有了,判断回文过程就需要用到exkmp了
例如:
s串:acacacac t串:cacacaca extend1[]
s串:cacacaca
t串:acacacac extend2[]
(此处exkmp的原理就不再说了,不懂的看前面的博客,谢谢)
为什么要求两个extend呢?
, 因为两个extend 第一个用来判断后缀是不是回文串, 第二个用来判断前缀是不是回文串。十分重要。
因为最后要输出价值,因此我们设置一个数组用来存前i个的价值总和, 最后求出自己想要的区间的价值。
细节不多说,直接上代码。
c 171ms
c++ 124ms
一下为c++版本
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
const int mx=500005;
char s[mx];
char t[mx];
int nt[mx],extend1[mx],extend2[mx];
int val[30],sum[mx];
int len,i,j,n;
void get_next(char *T,int *nt)
{
int a=0,p=0;nt[0]=len;// a是用来纪录上一个点的匹配的公共缀 p为当前字符的最远的匹配的下标
for(i=1;i<len;i++){//第一个元素的值设置为m总长度
if(i>=p||i+nt[i-a]>=p)
{
if(i>=p) p=i;//p如果小于i p只能从i处开始
while(p<len&&T[p]==T[p-i]) p++;//意思就是将主串t的第i个开始匹配 t从0开始的字符串 因此需要用t[p]和t[p-i]比较是否相等 若相等则将p最远距离拉远可以得到当前字符的与t串的最长公共前缀长了
nt[i]=p-i;//意思从当前i位置匹配了几个与t串的相同的前缀
a=i;
}
else nt[i]=nt[i-a];
}
}
void ekmp(char *S,char *T,int *nt,int *extend)
{
int a=0,p=0;
get_next(T,nt);
for(i=0;i<len;i++)
if(i>=p||i+nt[i-a]>=p){
if(i>=p) p=i;
while(p<len&&p-i<len&&S[p]==T[p-i]) p++;
extend[i]=p-i;
a=i;
}
else extend[i]=nt[i-a];
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
while(n--)
{
for(i=0;i<26;i++)cin>>val[i];
cin>>s;
memset(sum,0,sizeof(sum));
len=strlen(s);
for(i=0;i<len;i++){
sum[i+1]=val[s[i]-'a']+sum[i];//前i个的总和
t[i]=s[len-i-1];//s的反转串t
}
ekmp(s,t,nt,extend2);
ekmp(t,s,nt,extend1);
int maxx=-1000000000;
for(i=0;i<len;i++)
{
if(i&&extend1[i]+i==len)//判断前缀是不是回文串 第一个不做判断 因为要切割成两份
{
int pos=extend1[i];//前缀的回文长度,
int tmp=sum[pos];//前pos个的价值之和
if(extend2[pos]+pos==len) //看余出的串是不是回文串
tmp+=sum[len]-sum[pos];
if(tmp>maxx)
maxx=tmp;
}
else{//如果前半部分不是回文直接求后半部分
int pos=i+1,tmp=0;
if(extend2[pos]+pos==len)
tmp+=sum[len]-sum[pos];
if(tmp>maxx)
maxx=tmp;
}
}
cout<<maxx<<endl;
}
return 0;
}