hdu3613之KMP应用
来源:互联网 发布:清华体验知乎 编辑:程序博客网 时间:2024/06/08 00:04
Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 336 Accepted Submission(s): 131
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
首先输入n表示有n个测试,接着下一行有26个数分别表示a,b,c....z的价值,然后输入一行字符串,求将该字符串二分后的最大价值,二分后的字串如果是回文串则价值为字符价值之和,否则价值为0
另一种EKMP做法:http://blog.csdn.net/xingyeyongheng/article/details/9386235
另一种manacher做法:http://blog.csdn.net/xingyeyongheng/article/details/9386313
同种思路两种写法:
第一种(效率更高):
/*思路:本题难度在于如何将原串二分后判断前缀是否是回文和后缀是否是回文串则可以将原串反转得s2,然后用原串s1去匹配s2,最终s1匹配到的位置就是s1的最大前缀回文串同理用s2去匹配s1得到s2的最大前缀回文串,即s1得最大后悔回文串然后根据next[k]的特性,next[k]跳转到的位置就是下一个前缀回文串(0~k和x~len相同,而x~len又是0~k反转得到的,所以0~k是回文串)直到k=0,将所有前缀回文串和后缀回文串标记,然后对原串线性扫描求出最大值即可 */#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=500000+10;char s1[MAX],s2[MAX];int next[MAX],sum[MAX],val[27];int per[MAX],pos[MAX];//用来标记前缀回文串和后缀回文串void get_next(char *a,int len){int i=-1,j=0;next[0]=-1;while(j<len){if(i == -1 || a[i] == a[j])next[++j]=++i;else i=next[i];}}int KMP(char *a,char *b,int len){int i=0,j=0;while(i<len && j<len){if(i == -1 || a[i] == b[j])++i,++j;else i=next[i];}return i;}int main(){int n,k;cin>>n;while(n--){for(int i=0;i<27;++i)scanf("%d",&val[i]);scanf("%s",s1);int len=strlen(s1);for(int i=0;i<len;++i)s2[i]=s1[len-1-i],sum[i+1]=sum[i]+val[s1[i]-'a'];get_next(s1,len);k=KMP(s1,s2,len);//求s1最大前缀回文串位置while(k)per[k]=n+1,k=next[k];//标记所有前缀回文串 get_next(s2,len);k=KMP(s2,s1,len);//求s1最大后缀回文串位置 while(k)pos[k]=n+1,k=next[k];//标记所有后缀回文串 int ans=-INF,num=0;for(int i=1;i<len;++i){if(per[i] == n+1)num+=sum[i];if(pos[len-i] == n+1)num+=sum[len]-sum[i];if(num>ans)ans=num;num=0;}cout<<ans<<endl;}}第二种:
思路:将原串 + 一个字符(比如'#') + 原串反转后的串 得到新串s,对s求next,则next[len]为原串的回文前缀,一直next[len]则可得到所有回文前缀,然后求原串后缀,最后求结果
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=500000+10;char s[MAX*2];int next[MAX*2],sum[MAX],val[27];int pre[MAX],pos[MAX];//标记原串中的回文前缀和回文后缀 void get_next(char *a,int len){int i=-1,j=0;next[0]=-1;while(j<len){if(i == -1 || a[i] == a[j])next[++j]=++i;else i=next[i];}return;}int main(){int n;cin>>n;while(n--){for(int i=0;i<26;++i)scanf("%d",&val[i]);scanf("%s",s);int len=strlen(s),k=len,temp=len;for(int i=1;i<=len;++i)sum[i]=sum[i-1]+val[s[i-1]-'a'];s[len]='#';//中间用一个字符隔开则next[len]就一定在#前面,即寻找到的回文前缀一定是原串的回文前缀 while(k)s[++len]=s[--k];//将原串反转连接在原串后面 s[++len]='\0';get_next(s,len);k=len;while(next[k])pre[next[k]]=n+1,k=next[k];//标记前next[len]个字符是回文串,即标记回文前缀 for(int i=0;i<temp;++i){//前后两部分字串交换位置实现将最初原串反转连接在前面,为了求回文后缀 s[i]=s[i]^s[temp+1+i];s[temp+1+i]=s[i]^s[temp+1+i];s[i]=s[i]^s[temp+1+i];}get_next(s,len);k=len;while(next[k])pos[next[k]]=n+1,k=next[k];//标记前next[len]是回文串,即标记回文后缀int ans=-INF,num=0;for(int i=1;i<temp;++i){//求最大值 if(pre[i] == n+1)num+=sum[i];if(pos[temp-i] == n+1)num+=sum[temp]-sum[i];if(num>ans)ans=num;num=0;}cout<<ans<<endl;}return 0;}
- hdu3613之KMP应用
- hdu3613之扩展KMP
- HDU3613 Best Reward 3连发之KMP
- 【KMP】 hdu3613 Best Reward
- hdu3613(扩展KMP)
- 扩展KMP+hdu3613
- hdu3613 extend-kmp
- hdu3613 Best Reward(KMP)
- HDU3613 Best Reward 3连发之扩展KMP
- [扩展KMP][HDU3613][Best Reward]
- hdu3613之manacher算法
- HDU3613:Best Reward(拓展KMP)
- hdu3613 extend-kmp(Trie入门)
- HDU3613 Best Reward —— 扩展KMP
- HDU3613 Best Reward(扩展KMP)
- poj2185之KMP应用
- poj2406之kmp应用
- poj1961之KMP应用
- static_cast 与reinterpret_cast
- python快速入门视频教程
- 在Eclipse环境下开发Java Web项目
- 自主开发ERP的技术平台选择
- SCI与EI收录的外文期刊(计算机类)
- hdu3613之KMP应用
- 移动开发:android编程中遇到的Caused by: android.view.InflateException: Binary XML file line #11: Erro错误的原因及解决办法
- boa实例测试
- PLSQL Developer连接Oracle11g 64位数据库配置详解
- 复制文件或文件夹时出错(无法复制***:没有足够的可用磁盘空间。) .
- pache服务器实现301重定向-ASP重定向
- SQL常用语句总结
- Mono概述及部分源码解析
- As of ADT 14, resource fields cannot be used as switch cases