HDu 3613(拡張KMP)


Best Reward
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 417    Accepted Submission(s): 169
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: 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

 
Source
2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU
 
Recommend
lcy
 
この問題では、文字列の重み値と文字列のシーケンスを指定します.文字列の価値は、文字列のすべての文字の重み値とです.それ以外の場合は0です.文字列を2つの部分に分けて、2つの部分の重み値とを求めます.
         あるセグメントの文字列の重み値と私たちは累積と前処理を使用することができます.重要なのは、左右の重みと最大値を設定することです.左右の重み値と,左右の重み値とを前処理して求めることができ,すなわち左右の重みが文列であるかどうかを求め,その中で最も大きいものを選ぶことができる.
求回文列については、拡張KMPアルゴリズムやManacherアルゴリズムを使うことができますが、私はまずManacherアルゴリズムを使いますが、価値を求めるときに問題が発生しました.そこで拡張KMPアルゴリズムを用いた.
         まず、左端点から始まる文字列を処理し、現在、元の列の末尾に現れない文字を加えて区切ることができ、その後、元の列を逆に新しい列の後ろに加えることで、KMPアルゴリズムのnext[]配列によって左端点から始まる長さの異なる文字列を求めることができる.次に、右端から始まる回文列を求め、元の文字をひっくり返して新しい列を形成し、上の操作を繰り返します.
 
/*
#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("
"); for(i=1;i<len;i++) printf("%d ",suf[i]); printf("
"); 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
",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
",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
",ans); } return 0; }