用kmp的方法写了好久,发现以我的想法用kmp不仅难以实现而且无法证明正确性。 于是还是使用扩展kmp。。。
Best RewardTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 378 Accepted Submission(s): 151
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.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <math.h> #include <map> #include <queue> #include <sstream> #include <iostream> using namespace std; #define INF 0x3fffffff #define N 500500 char g[N],g1[N]; int next[N]; int chg[30]; int len; int sum[N]; int mark[N],mark1[N]; int num[N]; void build(char s[]) { memset(next,0,sizeof(next)); int p=0,a=0; next[0]=len; for(int i=1;i<len;i++) { if(i+next[i-a]-1<p) { next[i]=next[i-a]; } else { int k=p-i; while(p+1<len && s[p+1]==s[k+1]) { p++; k++; } p=max(p,i); next[i]=k+1; a=i; } } } void extend_kmp(char s[],char t[]) { memset(sum,0,sizeof(sum)); int p=-1,a=0; for(int i=0;i<len;i++) { if(i+next[i-a]-1<p) { sum[i]=next[i-a]; } else { int k=p-i; while(p+1<len && s[p+1]==t[k+1]) { p++; k++; } p=max(p,i); sum[i]=k+1; a=i; } } } int main() { //freopen("//home//chen//Desktop//ACM//in.text","r",stdin); //freopen("//home//chen//Desktop//ACM//out.text","w",stdout); int T; scanf("%d",&T); while(T--) { for(int i=0;i<26;i++) scanf("%d",chg+i); scanf("%s",g); len=strlen(g); for(int i=0;i<len;i++) { g1[len-1-i]=g[i]; } build(g); extend_kmp(g1,g); memset(mark,0,sizeof(mark)); memset(num,0,sizeof(num)); int tmp=0; for(int i=0;i<len;i++) { tmp+=chg[g[i]-'a']; num[i]=tmp; } for(int i=0;i<len-1;i++) { if(sum[len-1-i]==i+1) { mark[i]=1; } } memset(mark1,0,sizeof(mark1)); build(g1); extend_kmp(g,g1); for(int i=0;i<len-1;i++) { if(sum[len-1-i]==i+1) { mark1[i]=1; } } int mx=-INF; for(int i=0;i<len-1;i++) { tmp=0; int k=len-i-2; if(mark[i]==1) tmp+=num[i]; if(mark1[k]==1) tmp+=num[len-1]-num[i]; mx=max(mx,tmp); } printf("%d\n",mx); } return 0; }