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. mac 上如何安装非app store上的下载的软件-------打开未知来源

    打开了 Terminal 终端后 ,在命令提示后输入 sudo spctl --master-disable 并按下回车执行,如下图所示.   随后再输入当前 Mac 用户的密码,如下图所示.   如 ...

  2. JMeter实现唯一参数生成不重复时间戳

    现象: 使用jmeter做接口压测时,总会遇到压测时,提示不允许重复id或提示订单不允许重复现象,那么如何解决呢? 原料工具 jmeter4.0 本地准备好接口服务 思路: 单个接口,小批量接口,一般 ...

  3. 【MySql】删除操作

    删除表内数据,用 delete.格式为: delete from 表名 where 删除条件; 实例:删除学生表内姓名为张三的记录. delete from student where T_name ...

  4. WebSocket异步通讯,实时返回数据实例

    定义类中的异步方法 using System;using System.Collections.Generic;using System.IO;using System.Linq;using Syst ...

  5. checkstyle.xml Code Style for Eclipse

    1. Code Templates [下载 Code Templates] 打开 Eclipse -> Window -> Preferences -> Java -> Cod ...

  6. Confluence 6 Windows 中以服务方式自动重启的原因

    针对长时间使用的 Confluence,我们推荐你配置 Confluence 自动随操作系统重启而启动.针对一些 Windows 的服务器,这意味着需要让 Confluence 以服务的方式运行. 有 ...

  7. Rational Rose 2007下载、安装和破解

    一.文件下载 (1)DAEMON Tools Lite(虚拟光驱)下载地址 链接:https://pan.baidu.com/s/19L1FT6T1MlyhkfXyobd26A 提取码:drfs (2 ...

  8. ajax补充--------FormData等...

    一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000)  # 3000表示3秒,foo表示一个函数,3秒后 ...

  9. vue之指令

    一.什么是VUE? 它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 二.怎么使用VUE? 1.引入vue.js 2.展示HTML <div id=" ...

  10. python网络爬虫笔记(二)

    一.函数调用的默认设置 1.def enroll(name,grnder,age=4,city='Shanghai'): print (''name:',name) print (''gender', ...