hdu3613(扩展KMP)
来源:互联网 发布:取消数据库紧急模式 编辑:程序博客网 时间:2024/06/02 21:18
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.
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.
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
21 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 1acacac
Sample Output
16
Source
2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU
Recommend
lcy
本题给定字母的权值和一个字符序列,若为回文串,则字符串的价值为该字符串的所有字符权值和;否则为0。现在要把字符串分成两部分,求两部分的权值和。
对于某段字符串的权值和我们可以用累加和进行预处理。关键在于使左右两边的权值和最大。我们可以预处理求出左右两边的权值和,即求出左右两边是否为回文串,然后挑选其中最大的。
关于求回文串,可以用扩展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("\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;}
- hdu3613之扩展KMP
- hdu3613(扩展KMP)
- 扩展KMP+hdu3613
- [扩展KMP][HDU3613][Best Reward]
- HDU3613 Best Reward —— 扩展KMP
- HDU3613 Best Reward(扩展KMP)
- HDU3613 Best Reward 3连发之扩展KMP
- 【KMP】 hdu3613 Best Reward
- hdu3613之KMP应用
- hdu3613 extend-kmp
- hdu3613 Best Reward(KMP)
- HDU3613-----Best Reward-----用扩展KMP来判断某个串是不是回文串
- HDU3613:Best Reward(拓展KMP)
- hdu3613 extend-kmp(Trie入门)
- HDU3613 Best Reward 3连发之KMP
- 扩展KMP
- 扩展KMP
- 扩展KMP
- WIN7上实现一开机就自动连接宽带 && WIN7自带的系统备份和还原 && WIN7盘符混乱 && WIN7查看隐藏文件 && WIN7禁止提示更新 && WIN7备份和恢复
- 如何让table表的null列由不走索引变为可走索引
- hdu 题目1907 Johu (取火柴游戏,取最后一个胜)
- i.MX Yocto Project: ltib versus bitbake
- redis学习笔记三之排序
- hdu3613(扩展KMP)
- github简单使用
- redis学习笔记四之事务
- Java序列化Serializable和ObjectOutputStream
- 分布式数据库系列
- JS逻辑运算符
- ASP.NET MVC 2博客系列之模型验证
- 实际实验中的服务器平台评测
- iOS图片拉伸的3种常见技巧