HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)
题目链接:
https://cn.vjudge.net/problem/HDU-3613
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.
the number of test cases. The description of these test cases follows.
For each test case, the first line is 26 integers: v
1, v
2, ..., v
26 (-100 ≤ v
i ≤ 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 v
1, the value of 'b' is v
2, ..., and so on. The length of the string is no more than 500000.
OutputOutput 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
/*
题意描述
输入26个小写字母的价值,再输入一个字符串,问从哪个位置分割开,两个串的价值之和最大,当分成的串是回文串的时候,价值是各个字母的
和,不是回文串的时候,价值为0 解题思路
问题的关键是如何判断从哪个位置分割开后,前缀和后缀是不是回文串,普通的办法时间复杂度太高。
具体做法是将原s串反转赋值给t,拿s去匹配t,跑一遍kmp,得到s串的最大前缀子串的位置temp
同理拿t串去匹配s,跑一边kmp,得到s串的最大后缀子串的位置temp
由next数组的特性可知,将temp的位置标记为前缀回文后,寻找之前一个的前缀回文,也即next[temp]里面存的是之前一个前缀回文串的位置
,然后标记,如此循环,直到temp=0,表示没有前缀回文串,同理可得到后缀子串的回文标记数组。然后枚举每一个分割位置,判断是否时是
回文,计算求解最大值,其中利用前缀和数组加快计算。
*/
#include<cstdio>
#include<cstring> const int maxn = ;
const int inf=;
char s[maxn], t[maxn];
int val[], next[maxn], presum[maxn];
bool pre[maxn], pos[maxn]; void get_next(char b[], int len);
int kmp(char a[], char b[], int len); int main()
{
int T;
scanf("%d", &T);
while( T-- ){
for(int i = ; i < ; i++){
scanf("%d", &val[i]);
}
scanf("%s", s);
int len = strlen(s);
for(int i=;i<len;++i)
t[i] = s[len - - i],presum[i + ] = presum[i] + val[s[i] - 'a'];
t[len]='\0'; memset(next,,sizeof(next));
get_next(s,len);
int temp=kmp(s,t,len);//拿s去匹配t,也即t是主串,s是副串
memset(pre, , sizeof(pre));
while(temp){
pre[temp] = ;
temp = next[temp];
} memset(next,,sizeof(next));
get_next(t,len);
temp=kmp(t,s,len);
memset(pos, , sizeof(pos));
while(temp){
pos[temp] = ;
temp = next[temp];
} int ans = -inf;
for(int i = ; i < len; i++){
int sum = ;
if(pre[i])
sum += presum[i];
if(pos[len - i])
sum += presum[len] - presum[i];
if(sum > ans)
ans = sum;
}
printf("%d\n",ans);
}
return ;
} int kmp(char a[], char b[], int len){//注意a是副串,b是主串
int i=;
int j=;
while(i < len && j < len){
if(j == - || a[j] == b[i]){
i++;
j++;
}
else
j=next[j];
}
return j;//返回副串,也就是原串的最后匹配位置,故前j称为原串的最大前缀回文子串
} void get_next(char a[], int len){
int i = ;
int j = -;
next[] = -;
while(i < len){
if(j == - || a[i] == a[j]){
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
/*for(int i = 1; i <= len; i++){
printf("%d ",next[i]);
}
puts("");*/
}
HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)的更多相关文章
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- 力扣算法:125-验证回文串,131-分割回文串---js
LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- hdu 3613"Best Reward"(Manacher算法)
传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...
- Codeforces Round #410 (Div. 2) A. Mike and palindrome【判断能否只修改一个字符使其变成回文串】
A. Mike and palindrome time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU 5371(2015多校7)-Hotaru's problem(Manacher算法求回文串)
题目地址:HDU 5371 题意:给你一个具有n个元素的整数序列,问你是否存在这样一个子序列.该子序列分为三部分,第一部分与第三部分同样,第一部分与第二部分对称.假设存在求最长的符合这样的条件的序列. ...
- 算法 -- 四种方法获取的最长“回文串”,并对时间复杂进行分析对比&PHP
https://blog.csdn.net/hongyuancao/article/details/82962382 “回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就 ...
- (最长回文串 模板) 最长回文 -- hdu -- 3068
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory ...
随机推荐
- html5 定位
需要实现的功能:移动端的网页,能定位到中文地址. 百度地图能实现这样的功能. 之前精度差得原因是,我用自己的mac做服务器,用手机来浏览定位,这样只能定位到mac 的地址,mac上浏览器的地址就没准了 ...
- 编程中常用的DOS命令
1. dir directory 无参数:查看当前所在目录的文件和文件夹. /s : 查看当前目录以及其所有子目录的文件和文件夹 /a :查看包含的隐含文件的所有文件. /ah :只显示出隐含文 ...
- cocos 2dx 通过循环实现界面图形的摆放
首先创建一个一维数组 this.starSprites = new Array(); 然后知道星星的间距和坐标后通过如下代码实现位置的摆放 for(var i = 0; i < 6; i++){ ...
- Excel 多个单元格输入同样内容
step1: 将这些单元格选定.方法:可以连续选,也可以 ctrl + select不连续选择: step2:输入你想输入的内容,PS:出现在最后选择的单元格中: step3:组合键:ctrl + e ...
- 使用更改跟踪(ChangeTracking)来实现数据类型变更
在现实场景中,我们经常会遇到修改数据类型的场景,尤其是自增列从INT修改为BIGINT的情况,自增列又通常作为表的主键和聚集索引键,因此修改操作需要按以下步骤来进行 1. 停止对该表的访问(通过禁用权 ...
- ABP框架入门踩坑-配置数据库表前缀
配置数据库表前缀 ABP踩坑记录-目录 本篇其实和ABP关系并不大,主要是EF Core的一些应用-.-. 起因 支持数据库表前缀应该是很多应用中比较常见的功能,而在ABP中并没直接提供这一功能,所以 ...
- Bootstrap框架下实现图片切换
准备图片,把相关记录添加至数据库表中: 创建一个存储过程,获取所有记录: 在ASP.NET MVC专案中,部署Bootstrap环境...... 然后创建一个model: using System; ...
- 关于GROUP BY和聚合函数
可以这样去理解group by和聚合函数 转自 http://www.cnblogs.com/wiseblog/articles/4475936.html 写在前面的话:用了好久group by,今天 ...
- 百度地图sdk---pc端
<div class="map" style="width: 1196px;height: 500px;margin: 50px auto;"> & ...
- cnetos6上实现nfs共享
利用空余时间,做个nfs共享实验,岂不美滋滋!!! 系统测试环境: 主机ip 系统 主机名(服务) 192.168.241.130 centos6.5 hadoop1(nfs-sr ...