Hdu 3613 exkmp, hduexkmp
Question Link
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 3613
Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 940 Accepted Submission (s): 390
Problem DescriptionAfter an uphill battle, General Li won a great vicures. Now the head of state decide to reward him with honor and treasures for his great exploit.
One of these treasures is a neck?made up of 26 different kinds of gemstones, and the length of the neck+is n. (That is to say: n gemstones are stringed together to constitute this necktasks, and each of these gemstones belongs to only one of the 26 kinds .)
In accordance with the classical view, a neckpattern is valuable if and only if it is a palindrome-the neck1_looks the same in either direction. however, the neckaskwe 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 neckrom that is not palindrom has value zero.
Now the problem is: how to cut the given neck1_so that the sum of the two necklaces's value is greatest. Output this value.
InputThe 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 necktasks. 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.
OutputOutput a single Integer: the maximum value General Li can get from the neck.pdf.
Sample Input21 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 1 1aba1 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 1 1 1 1 1 acacac Sample Output1 6 original string prefix and (reverse string obtained after the original string is reversed) if the prefix length is the same, the character corresponding to the original string is the return string. After simulating ex [I], you can obtain ex [I] + I = len. If the first half of the paragraph (to I) is not a text return, you can directly judge (I + 1) in the future, whether it is a text return.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <stack>using namespace std;const int inf=0x3fffffff;const int maxn=500000+10;char str1[maxn],str2[maxn];int nt[maxn],ex1[maxn],ex2[maxn],dp[maxn];int kind[26];int slen,tlen;void get_next(char *s){ nt[0]=slen; int l,r; for(l=0;l<slen-1&&s[l]==s[l+1];l++); nt[1]=l; l=1; for(int i=2;i<slen;i++) { r=l+nt[l]-1; if(nt[i-l]+i-1>=r) { int p=r-i+1>0?r-i+1:0; while(p+i<slen&&s[p]==s[p+i]) p++; nt[i]=p; l=i; } else nt[i]=nt[i-l]; }}void get_extand(char *s,char *t,int *ex){ memset(nt,0,sizeof(nt)); get_next(s); int l=0,r; while(l<slen&&s[l]==t[l]) l++; ex[0]=l; l=0; for(int i=1;i<tlen;i++) { r=ex[l]+l-1; if(nt[i-l]+i-1>=r) { int p=r-i+1>0?r-i+1:0; while(p+i<tlen&&s[p]==t[i+p]) p++; ex[i]=p; l=i; } else ex[i]=nt[i-l]; }}int main(){ //freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--) { for(int i=0;i<26;i++) scanf("%d",&kind[i]); scanf("%s",str1); memset(dp,0,sizeof(dp)); dp[0]=0; tlen=slen=strlen(str1); for(int i=1;i<=slen;i++) dp[i]=dp[i-1]+kind[str1[i-1]-'a']; for(int i=slen-1,j=0;i>=0;i--,j++) str2[j]=str1[i]; str2[slen]='\0'; get_extand(str1,str2,ex1); get_extand(str2,str1,ex2); int ans=0; for(int i=0;i<slen;i++) { int temp=0; if(i&&ex1[i]+i==slen) { int p=ex1[i]; temp+=dp[p]; if(ex2[p]+p==slen) { temp+=dp[slen]-dp[p]; } } else { int p=i+1; if(ex2[p]+p==slen) temp+=dp[slen]-dp[p]; } ans=ans>temp?ans:temp; //printf("i->%d ans->%d\n",i,ans); } printf("%d\n",ans); } return 0;}