首页 > ACM题库 > HDU-杭电 > hdu 3613-best reward-kmp-[解题报告]hoj
2014
11-27

hdu 3613-best reward-kmp-[解题报告]hoj

Best Reward

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



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
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

题意:首先输入n表示有n个测试,接着下一行有26个数分别表示a,b,c….z的价值,然后输入一行字符串,求将该字符串二分后的最大价值,二分后的字串如果是回文串则价值为字符价值之和,否则价值为0

分析:将原串s1反转得到s2,然后进行s1,s2扩展KMP匹配,得到extend,对于s1的前i个字符如果和s2的后i个字符相等即extend[len-i] == i则前i个字符为回文串,判断后len-i个字符是否是回文串用s2,s1进行扩展KMP即可

扩展KMP详解:http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html

另一种KMP做法:http://blog.csdn.net/xingyeyongheng/article/details/9292449

另一种manacher做法:http://blog.csdn.net/xingyeyongheng/article/details/9386313

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=500000+10;
char s1[MAX],s2[MAX];
int next[MAX],extend1[MAX],extend2[MAX];
int sum[MAX],val[27];

void get_next(char *a,int len){
	int k=0,i=1;
	next[0]=len;//可有可无,因为用不上 
	while(k+1<len && a[k] == a[k+1])++k;
	next[1]=k;//这里预先算好next[1]是因为不能k=0,否则next[i-k]=next[i]不是已算好的 
	k=1;
	while(++i<len){//和EKMP的过程一样 
		int maxr=k+next[k]-1;
		next[i]=min(next[i-k],max(maxr-i+1,0));//这里是扩展KMP的精髓,即算法核心思想就是这
		while(i+next[i]<len && a[next[i]] == a[i+next[i]])++next[i];
		if(i+next[i]>k+next[k])k=i; 
	}
}

void EKMP(char *a,char *b,int *extend,int len){
	get_next(a,len);
	int k=0,i=0;
	while(k<len && a[k] == b[k])++k;
	extend[0]=k;
	k=0;
	while(++i<len){
		int maxr=k+extend[k]-1;
		extend[i]=min(next[i-k],max(maxr-i+1,0));//next[i-k]是a与b从i开始的可能已经匹配的长度
		while(i+extend[i]<len && a[extend[i]] == b[i+extend[i]])++extend[i];//这里是扩展KMP的精髓,即算法核心思想就是这
		if(i+extend[i]>k+extend[k])k=i; 
	}
}

int main(){
	int n;
	cin>>n;
	while(n--){
		for(int i=0;i<26;++i)cin>>val[i];
		scanf("%s",s1);
		int len=strlen(s1);
		for(int i=1;i<=len;++i){
			sum[i]=sum[i-1]+val[s1[i-1]-'a'];
			s2[i-1]=s1[len-i];
		}
		EKMP(s1,s2,extend1,len);
		EKMP(s2,s1,extend2,len);
		int ans=0,temp=0;
		for(int i=1;i<len;++i){
			if(extend1[len-i] == i)temp+=sum[i];//表示前i个字符是回文串
			if(extend2[i] == len-i)temp+=sum[len]-sum[i];//表示后len-i个字符为回文串
			if(temp>ans)ans=temp;
			temp=0; 
		}
		cout<<ans<<endl;
	}
	return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。