用kmp的方法写了好久,发现以我的想法用kmp不仅难以实现而且无法证明正确性。 于是还是使用扩展kmp。。。

Best Reward

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

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
Source
2010 ACM-ICPC Multi-University Training Contest(18)——Host by TJU
Recommend
lcy
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff
#define N 500500char g[N],g1[N];
int next[N];
int chg[30];
int len;
int sum[N];
int mark[N],mark1[N];
int num[N];void build(char s[])
{memset(next,0,sizeof(next));int p=0,a=0;next[0]=len;for(int i=1;i<len;i++){if(i+next[i-a]-1<p){next[i]=next[i-a];}else{int k=p-i;while(p+1<len && s[p+1]==s[k+1]){p++; k++;}p=max(p,i);next[i]=k+1;a=i;}}
}void extend_kmp(char s[],char t[])
{memset(sum,0,sizeof(sum));int p=-1,a=0;for(int i=0;i<len;i++){if(i+next[i-a]-1<p){sum[i]=next[i-a];}else{int k=p-i;while(p+1<len && s[p+1]==t[k+1]){p++; k++;}p=max(p,i);sum[i]=k+1;a=i;}}
}int main()
{//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);//freopen("//home//chen//Desktop//ACM//out.text","w",stdout);int T;scanf("%d",&T);while(T--){for(int i=0;i<26;i++)scanf("%d",chg+i);scanf("%s",g);len=strlen(g);for(int i=0;i<len;i++){g1[len-1-i]=g[i];}build(g);extend_kmp(g1,g);memset(mark,0,sizeof(mark));memset(num,0,sizeof(num));int tmp=0;for(int i=0;i<len;i++){tmp+=chg[g[i]-'a'];num[i]=tmp;}for(int i=0;i<len-1;i++){if(sum[len-1-i]==i+1){mark[i]=1;}}memset(mark1,0,sizeof(mark1));build(g1);extend_kmp(g,g1);for(int i=0;i<len-1;i++){if(sum[len-1-i]==i+1){mark1[i]=1;}}int mx=-INF;for(int i=0;i<len-1;i++){tmp=0;int k=len-i-2;if(mark[i]==1)tmp+=num[i];if(mark1[k]==1)tmp+=num[len-1]-num[i];mx=max(mx,tmp);}printf("%d\n",mx);}return 0;
}

hdu3613(扩展KMP)相关推荐

  1. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

  2. 嫦娥奔月(KMP,找循环节)及其扩展KMP

    问题描述 <归妹>卦辞为:昔者恒我(姮娥)窃毋死之药于西王母,服之以(奔)月.将往,而枚占于有黄.有黄占之曰:"吉.翩翩归妹,独将西行.逢天晦芒,毋惊毋恐,后且大昌". ...

  3. HDU 3613 Best Reward 正反两次扩展KMP

    题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...

  4. HDU - 4333 Revolving Digits(扩展KMP)

    题目链接:点击查看 题目大意:给出一个由 n 个数位组成的数字,现在可以通过将其不同的后缀移到前面来组成 n 个新的数字,现在要求出 n 个新数字进行去重后,有多少个新数字分别大于.等于.小于原数字 ...

  5. HDU - 4300 Clairewd’s message(扩展KMP)

    题目链接:点击查看 题目大意:给出两个字符串 s 和 t ,字符串 s 代表着一种密码的映射,字符串 t 代表着一段密文+明文,题目保证密文是完整的,但明文只有一部分,现在问如何补全字符串 t ,使得 ...

  6. HDU - 6629 string matching(扩展KMP)

    题目链接:点击查看 题目大意:给出一个字符串 s 和一个暴力程序,用于求解 s 的每个后缀和原字符串的最长公共前缀,现在问一共需要执行多少次比较操作 题目分析:首先肯定不能暴力去模拟,时间复杂度n*n ...

  7. HDU - 6153 A Secret(KMP的next数组性质/扩展KMP)

    题目链接:点击查看 题目大意:给出两个字符串a和b,我们首先定义L:字符串b当前的后缀子字符串长度,N:字符串b当前的后缀在字符串a中出现的次数,现在询问,对于字符串b的每一个后缀,求出L*N之和,答 ...

  8. A - A Secret -扩展KMP

    题目大意: 给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少. 题解: 扩展KMP模板题,将A和B串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最 ...

  9. UVA5876 Writings on the Wall 扩展KMP

    扩展KMP的简单题. #include<stdio.h> #include<string.h> #define maxn 51010 char s[maxn],t[maxn]; ...

最新文章

  1. elastic date时区问题解决办法
  2. 快速理解 session/token/cookie 认证方式
  3. html5 --- 利用localStorage进行本地存储
  4. ITU-R BT.656 协议
  5. ASP.NET Core MVC 授权的扩展:自定义 Authorize 和 IApplicationModelProvide
  6. 《大数据》2015年第2期“专题”——关于大数据交易核心法律问题 —— 数据所有权的探讨及建议...
  7. java的mysql语句规范_常用的标准SQL 语句
  8. 赔了24亿,干到中国第一!最“不要命”航班,竟让无数人抢着坐?
  9. iPhone 12或支持全新短距离WiFi标准 数据传输更快
  10. python如何编辑pdf_python3.6 导入并编辑pdf 错误在哪里啊
  11. 今日恐慌与贪婪指数为74 等级转为贪婪
  12. innodb_file_format设置
  13. 格式当前时间mongodb date type
  14. 五大开源MySQL管理工具!
  15. 计算机类课题研究方法,课题研究的主要内容和研究方法.doc
  16. Exception in thread main java.lang.UnsatisfiedLinkError: com.mzlr.jni.Calculater.Sum(II)I at com.
  17. 2020年上半年我国互联网网络安全监测数据分析报告
  18. 精通CSS-网页排版
  19. s5p4418显示驱动
  20. 魔兽3的地图脚本文件简单分析图

热门文章

  1. pwm波如何控制电机代码_柴油发电机机组的控制器该如何操作
  2. 控制显示隐藏_iOS13隐藏了5个超实用新功能:让iPhone的使用体验更好
  3. html5和响应式,35个响应式HTML5和CSS3模版
  4. 解密优秀博士成长史 ——微软亚洲研究院首届博士生学术论坛Panel讨论经验总结
  5. Oracle编程入门经典 第11章 过程、函数和程序包
  6. java学习笔记6--类的继承、Object类
  7. 压缩感知(III) A Compressed Sense of Compressive Sensing (III)
  8. matlab显示的图片,手动保存时四周有白边
  9. 给在读研究生的一封信(强烈推荐)
  10. Machine Learning week 5 quiz: Neural Networks: Learning