Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 417 Accepted Submission(s): 169
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.
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.
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
1 6
/* #include<iostream> #include<cstring> #include<cstdio> using namespace std; //***************************************************************** const int MAXN=500000+100; char str1[MAXN*2],str2[MAXN*2];//待处理字符串 int num[MAXN*2]; int value[30]; int sum[MAXN]; int pre[MAXN]; int suf[MAXN]; //将str1变成str2,如abab变成$#a#b#a#b# void init() { int i,id; str2[0]='$';sum[0]=0; str2[1]='#';sum[1]=0; for(i=0,id=2;str1[i];i++,id+=2) { str2[id]=str1[i];sum[id]=sum[id-1]+value[str1[i]-'a']; str2[id+1]='#';sum[id+1]=sum[id]; } str2[id]=0;sum[id]=sum[id-1]; } //Manacher算法求最长回文子串,时间复杂度为O(N) void Manacher() { int i,ans=0,MxR=0,pos,len=strlen(str2); memset(pre,0,sizeof(pre));memset(suf,0,sizeof(suf)); for(i=1;str2[i];i++) { if(MxR>i)num[i]=num[pos*2-i]<(MxR-i)?num[pos*2-i]:(MxR-i); else num[i]=1; while(str2[i+num[i]]==str2[i-num[i]]) num[i]++; if(num[i]+i>MxR) { MxR=num[i]+i; pos=i; } if(i-num[i]==0)pre[i+num[i]-1]=1;//表示前缀(前p[i]-1个字符)是回文串 if(i+num[i]==len)suf[i-num[i]+1]=1;//表示后缀(后p[i]-1个字符)是回文串 } } //***************************************************************** int main() { int cas,i,len,tmp,ans; cin>>cas; while(cas--) { for(i=0;i<26;i++) scanf("%d",&value[i]); scanf("%s",str1); init(); Manacher(); len=strlen(str2); for(i=1;i<len;i++) printf("%d ",pre[i]); printf("\n"); for(i=1;i<len;i++) printf("%d ",suf[i]); printf("\n"); ans=1; for(i=1;i<len;i++) { tmp=0; if(pre[i]&&i+num[i]<len)tmp+=sum[i-1]; if(suf[i]) tmp+=sum[i+num[i]]-sum[i-1]; if(ans<tmp)ans=tmp; } printf("%d\n",ans); } return 0; } */ #include<iostream> #include<cstring> #include<cstdio> using namespace std; const int MAXN=(500000+100)*2; int value[MAXN],sum[MAXN],pre[MAXN],suf[MAXN]; char str[MAXN]; //***************************************************************** int next[MAXN]; //KMP算法中计算next[]数组 void getNext(char *p) { int j,k,len=strlen(p); j=0; k=-1; next[0]=-1; while(j<len) { if(k==-1||p[j]==p[k]) { next[++j]=++k; } else k=next[k]; } } //***************************************************************** int main() { int cas,i,len,tlen,ans,k,tmp; char ch; //freopen("in.txt","r",stdin); cin>>cas; while(cas--) { for(i=0;i<26;i++) scanf("%d",&value[i]); scanf("%s",str); len=strlen(str); sum[0]=value[str[0]-'a']; for(i=1;i<len;i++) sum[i]=sum[i-1]+value[str[i]-'a']; //sum[i]=sum[i-1]; tlen=len*2+1; str[len]='#'; for(i=0;i<len;i++) { str[len+1+i]=str[len-1-i]; } str[len+len+1]=0; // printf("str=%s\n",str); getNext(str); k=next[tlen]; while(k!=0) { pre[k]=cas+2; k=next[k]; } tlen=len*2+1; for(i=0;i<len/2;i++) { ch=str[i]; str[i]=str[len-i-1]; str[len-i-1]=ch; } for(i=0;i<len;i++) { str[len+1+i]=str[len-1-i]; } getNext(str); k=next[tlen]; while(k!=0) { suf[k]=cas+2; k=next[k]; } int ans=-99999999; for(i=1;i<len;i++) { tmp=0; if(pre[i]==cas+2)tmp+=sum[i-1]; if(suf[len-i]==cas+2)tmp+=(sum[len-1]-sum[i-1]); if(ans<tmp)ans=tmp; } printf("%d\n",ans); } return 0; }