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
本来以为kmp可以判断回文  但是遇到 abba  next 就不能判断了
应该先验证思路再开始敲代码的  还调试了半天
。。。
 
参考了大佬的做法:
要用拓展ekmp来求回文串
 

分析:

首先原始串为S,将S逆转得到串T.(S=abcaaa,那么T=aaacba).

S串的后缀回文:即S串中区间[i,n-1]的串是不是回文?

  

将S作为主串,T串用扩展KMP算法去匹配S,extend1[n]数组保存匹配结果.如果extend1[i]+i==n时(n为S的长),那么以S[i]为首字符一直到底n-1位置的串是回文串,否则不是.(自己举个例子验证一下)

S串的前缀回文:即S串中区间[0,i-1]的串是不是回文?

将T作为主串,S串用扩展KMP算法去匹配T,extend2[n]数组保存匹配结果.如果extend2[len-i]+len-i==n时(n为S的长),那么以S[i-1]为尾字符一直到0位置的串是回文串,否则不是.(自己举个例子验证一下)

仔细思考下上面的模型.

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std; void EKMP(char s[],char t[],int nex[],int extend[])//s为主串,t为模板串
{
int i,j,p,L;
int lens=strlen(s);
int lent=strlen(t);
nex[]=lent;
j=;
while(j+<lent && t[j]==t[j+])j++;
nex[]=j; int a=;
for(i=;i<lent;i++)
{
p=nex[a]+a-;
L=nex[i-a];
if(i+L<p+)nex[i]=L;
else
{
j=max(,p-i+);
while(i+j<lent && t[i+j]==t[j])j++;
nex[i]=j;
a=i;
}
} j=;
while(j<lens && j<lent && s[j]==t[j])j++;
extend[]=j;
a=;
for(i=;i<lens;i++)
{
p=extend[a]+a-;
L=nex[i-a];
if(L+i<p+)extend[i]=L;
else
{
j=max(,p-i+);
while(i+j<lens && j<lent && s[i+j]==t[j])j++;
extend[i]=j;
a=i;
}
}
} const int MAXN=;
char str1[MAXN],str2[MAXN];
int sum[MAXN];
int v[];
int nex[MAXN];
int extend1[MAXN],extend2[MAXN]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=;i<;i++)
scanf("%d",&v[i]);
scanf("%s",str1);
int len=strlen(str1);
sum[]=;
for(int i=;i<len;i++)
{
sum[i+]=sum[i]+v[str1[i]-'a'];
str2[i]=str1[len--i];
}
str2[len]=;
EKMP(str2,str1,nex,extend1);
EKMP(str1,str2,nex,extend2);
int ans=-;
//需要保证分成两部分,所以i从1到len-1
for(int i=;i<len;i++)
{
int tmp=;
if(i+extend1[i]==len)
{
tmp+=sum[len-i];
}
int pos=len-i;
if(pos+extend2[pos]==len)
{
tmp+=sum[len]-sum[pos];
}
if(tmp>ans)ans=tmp;
}
printf("%d\n",ans);
}
return ;
}
 
 
 
 
 
 
 
 
 
 

Best Reward 拓展kmp的更多相关文章

  1. HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )

    题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...

  2. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  3. HDU - 3613 Best Reward(manacher或拓展kmp)

    传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...

  4. hdu-4300(kmp或者拓展kmp)

    题意:乱七八糟说了一大堆,就是先给你一个长度26的字符串,对应了abcd....xyz,这是一个密码表.然后给你一个字符串,这个字符串是不完整的(完整的应该是前半部分是加密的,后半部分是解密了的),然 ...

  5. hdu-4763(kmp+拓展kmp)

    题意:给你一个串,问你满足最大字串既是前后缀,也在字符串除去前后缀的位置中出现过: 思路:我用的是拓展kmp求的前后缀,只用kmp也能解,在字符串2/3的位置后开始遍历,如果用一个maxx保存前2/3 ...

  6. poj-2752(拓展kmp)

    题意:求一个串所有的前后缀字串: 解题思路:kmp和拓展kmp都行,个人感觉拓展kmp更裸一点: 拓展kmp: #include<iostream> #include<algorit ...

  7. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  8. 拓展KMP算法详解

    拓展KMP解决的问题是给两个串S和T,长度分别是n和m,求S的每一个后缀子串与T的最长公共前缀分别是多少,记作extend数组,也就是说extend[i]表示S[i,n-1](i从0开始)和T的最长公 ...

  9. KMP&拓展KMP

    KMP算法 说明 KMP算法是一种比较高效的字符串匹配算法,可以在线性时间内求出一个串在另一个串的所有匹配位置. 解析 详解KMP 设模板串是 \(pattern\) 令 \(next[i] = ma ...

随机推荐

  1. Go斐波拉契数列(Fibonacci)(多种写法)

    1 前言 斐波拉契数列有递归写法和尾递归和迭代写法. 2 代码 //recursion func fib(n int) int{ if n < 2{ return n }else{ return ...

  2. Android启动模式之singleinstance的坑

    前言 在实际应用中,使用singleinstance启动模式时,会遇到一些奇奇怪怪的问题.Android有四种启动模式,分别是standard,singleTop,singleTask,singleI ...

  3. 修改html中button显示的文字

    1. <input type="button">  实现密码输入框的可见和隐藏 直接修改value属性即可 <script type="text/jav ...

  4. 将Maven项目打包成可执行jar文件(引用第三方jar)

    方法一. mvn assembly 或 mvn package (一个jar包) 把依赖包和自己项目的文件打包如同一个jar包(这种方式对spring的项目不支持) <build>     ...

  5. J2SE基础小结

    1. 九种基本数据类型的大小,以及他们的封装类. 类型 基本类型 大小(字节) 默认值 封装类 整数型 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 ...

  6. JAVA菜鸟入门HelloWorld

    一:HelloWorld进入菜鸟心中 1.最原始的一切从HelloWorld开始 首先本机安装JDK,配置好path环境变量 用文本编辑器editplus或notepad++创建一个HelloWorl ...

  7. Pycharm同步本地代码至GitHub

    注册github账号 github地址,进入注册账号 安装git Windows下载地址1 Windows下载地址2 在官方下载完后,双击exe文件进行安装,安装到Windows Explorer i ...

  8. linux ssh远程免密码登入

    首先登入一台linux服务器,此台做为母机(即登入其他linux系统用这台做为入口):执行一行命令生成key文件:ssh-keygen -t rsa 2 在母机上,进入/roo/.ssh目录,找到id ...

  9. java读取.txt文件工具类FileUtiles

    public class FileUtils { private static final String ENCODING = "UTF-8";//编码方式 /** * 获取文件的 ...

  10. 论文阅读笔记二十四:Rich feature hierarchies for accurate object detection and semantic segmentation Tech report(R-CNN CVPR2014)

    论文源址:http://www.cs.berkeley.edu/~rbg/#girshick2014rcnn 摘要 在PASCAL VOC数据集上,最好的方法的思路是将低级信息与较高层次的上下文信息进 ...