首页 > 代码库 > HDU3613 Best Reward

HDU3613 Best Reward

 

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2028    Accepted Submission(s): 819


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. 

 

 

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: v1, v2, ..., v26 (-100 ≤ vi ≤ 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 v1, the value of ‘b‘ is v2, ..., 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
21 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
16
 

 

Source
2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU
 

 

Recommend
lcy
 
将一个字符串从中间截断,若截成的小段是回文串,其得分等于每个字母的得分和;若不是回文串,该段得分为1
 
 
用manacher处理出每个点的回文半径。若截出的串中点的回文长度大于串长,则这是回文串。枚举断点记录最大得分即可。
 1 /**/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 using namespace std; 8 const int mxn=1000200; 9 char s[mxn];10 char c[mxn];11 int w[30];12 int p[mxn],sum[mxn];13 int len,ans=0;14 void mana(){15     int id=0,mx=0;16     int i,j;17     for(i=1;i<len;i++){18         if(mx>i)p[i]=min(p[2*id-i],p[id]+id-i);19         else p[i]=0;20         while(s[i+p[i]]==s[i-p[i]])p[i]++;21         if(p[i]+i>mx){22             mx=p[i]+i;23             id=i;24         }25     }26     return;27 }28 int main(){29     int T;30     scanf("%d",&T);31     int i,j;32     while(T--){33         for(i=0;i<26;i++)scanf("%d",&w[i]);34         scanf("%s",c+1);35         len=strlen(c+1);36         sum[0]=0;37         for(i=1;i<=len;i++)sum[i]=sum[i-1]+w[c[i]-a];38         s[0]=$;s[1]=#;39         for(i=1;i<=len;i++){s[i*2]=c[i];s[i*2+1]=#;}40         len=len*2+2;s[len]=@;41         mana();42         ans=0;43         len=(len-2)/2;44         for(i=1;i<len;i++){45             int tmp=0;46             int l=(1+i*2+1)/2,r=(i*2+1+len*2+1)/2;47             if(p[l]>=i*2+1-l) tmp+=sum[i];48             if(p[r]>=r-(i*2+1))tmp+=sum[len]-sum[i];49 //            printf("i:%d tmp:%d\n",i,tmp);50             ans=max(ans,tmp);51         }52         printf("%d\n",ans);53     }54 }

 

HDU3613 Best Reward