hdu3613之扩展KMP
来源:互联网 发布:怎么贿赂淘宝小二 编辑:程序博客网 时间:2024/06/07 00:27
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.
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
分析:将原串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 99999999using 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;}
- hdu3613之扩展KMP
- hdu3613(扩展KMP)
- 扩展KMP+hdu3613
- hdu3613之KMP应用
- HDU3613 Best Reward 3连发之扩展KMP
- [扩展KMP][HDU3613][Best Reward]
- HDU3613 Best Reward —— 扩展KMP
- HDU3613 Best Reward(扩展KMP)
- HDU3613 Best Reward 3连发之KMP
- 【KMP】 hdu3613 Best Reward
- hdu3613 extend-kmp
- hdu3613 Best Reward(KMP)
- HDU3613-----Best Reward-----用扩展KMP来判断某个串是不是回文串
- hdu3613之manacher算法
- hdu4333之扩展KMP
- HDU3613:Best Reward(拓展KMP)
- hdu3613 extend-kmp(Trie入门)
- 2017暑假训练之KMP、扩展KMP
- sharepoint scripts
- VBS to query MIIS SQL for user profile that have special character
- 华为软件编程规范学习(二)--注释
- acm经验
- import user profile into UPS
- hdu3613之扩展KMP
- 华为软件编程规范学习(三)--标识符命名
- Generate version size report
- 打印浮点值
- Android --- 手游项目中聊天表情的处理(一)
- Java基本数据类型和长度
- 输入一个整数n,求从1 到n 这n 个整数的十进制表示中1 出现的次数
- oracle-java7-installer 中断错误
- KPI Generation