hdu 3613 Best Reward
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个字母的value,然后给出一个字符串,把字符串分成两段,每段不为空,对于每段的value,如果字符串是回文就是各个字符value和,如果不是则为0,求最大value和。
这里用kmp算法协助判断回文串,首先判断s的前缀,把s反转加到s后面,变换后的s本身就是一个回文串了,然后一层一层往里找回文串,这样判断的最长相同前缀和后缀一定是回文串,因为他和自己反转后相同啊(回文串从中间分开,后半段是前半段的反转)。
由于s的长度翻倍了,所以对于切割的部位,应该原s串内,即在slen内,这里sum_value记录前缀是回文串的位置的sum(value前缀和),然后把原s反转一下,接下来就是找后缀了,同样的过程,只不过切割的位置不同,如果某个后缀是回文,切割的位置是 slen - 后缀位置(函数中flag为1的情况)。 代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
char s[];///字符串
int Next[];
int sum[];///value前缀和
int value[],slen;
int sum_value[];///记录从某位置切开后的value总和
int ans;
void getNext() {///确立Next数组
Next[] = -;
int len = slen * ;
int j = -,i = ;
while(i < len) {
if(j == - || s[i] == s[j]) Next[++ i] = ++ j;
else j = Next[j];
}
}
void check(int flag){
sum[] = ;
for(int i = ;i < slen;i ++)///计算前缀和
sum[i + ] = sum[i] + value[s[i] - 'a'];
reverse_copy(s,s + slen,s + slen);///把s反转添加到s后面
getNext();///确立此时的Next数组
int len = slen * ;
while(len) {
if(len < slen) {
int cut = len;
if(flag) cut = slen - len;
sum_value[cut] += sum[len];
ans = max(ans,sum_value[cut]);
}
len = Next[len];
}
}
int main() {
int T;
scanf("%d",&T);
while(T --) {
for(int i = ;i < ;i ++)
scanf("%d",&value[i]);
scanf("%s",s);///读入s
slen = strlen(s);
memset(sum_value,,sizeof(sum_value));
ans = ;
check();
reverse(s,s + slen);///反转一下
check();
printf("%d\n",ans);
}
}
先计算前缀,sumval[i]就代表从i后切开的value总和,manacher算法可以求得所有的回文子串,如果回文串是前缀,便在回文串长度下标对应位置加上前缀和,如果是后缀,便在后缀的前一位对应位置加上后缀的value和,如此找寻最大值。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#define MAX 500000
using namespace std;
int v[],sum[MAX + ];
char t[MAX * + ] = {'@','#'};
int p[MAX * + ];
int sumval[MAX + ];
int manacher(char *s,int len) {
int c = ,ans = ;
for(int i = ;i < len;i ++) {
t[c ++] = s[i];
t[c ++] = '#';
}
int rp = ,rrp = ;
for(int i = ;i < c;i ++) {
p[i] = i < rrp ? min(p[rp - (i - rp)],rrp - i) : ;
while(t[i + p[i]] == t[i - p[i]]) {
p[i] ++;
}
if(i - p[i] == ) sumval[(i + p[i] - ) / ] += sum[(i + p[i] - ) / ];
else if(i + p[i] == len * + ) sumval[(i - p[i]) / ] += sum[len] - sum[(i - p[i]) / ];
if(rrp < i + p[i]) {
rrp = i + p[i];
rp = i;
}
}
for(int i = ;i < len;i ++) {
ans = max(ans,sumval[i]);
}
return ans;
}
int main() {
int t;
char s[MAX + ];
scanf("%d",&t);
while(t --) {
for(int i = ;i < ;i ++) {
scanf("%d",&v[i]);
}
scanf("%s",s);
int len = strlen(s);
for(int i = ;i <= len;i ++) {
sum[i] = sum[i - ] + v[s[i - ] - 'a'];
sumval[i] = ;
}
printf("%d\n",manacher(s,len));
}
}
hdu 3613 Best Reward的更多相关文章
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU - 3613 Best Reward(manacher或拓展kmp)
传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- HDU 3613 Best Reward(扩展KMP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- hdu 3613 Best Reward (manachar算法)
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
随机推荐
- 3.11 Templates --Rendering with Helpers
Ember提供几个辅助器允许你使用不同的方法渲染模板(render templates). 一.The {{partial}} Helper {{partial}}以呈现的模板作为参数,并在这里呈现模 ...
- python 中使用ConfigParser类修改配置文件
配置文件的格式: [user] user_ip=127.0.0.1 user_name=testuser user_id=13 import ConfigParser conf = ConfigPar ...
- Android 自定义View-字母索引表(一)
在有些Android应用中,为了方便快速定位,经常会看到屏幕右侧有一个字母索引表,今天尝试使用自定义View的方式实现了索引表的基本布局. 字母索引表的样式如下面的示意图所示, 此时我们至少需要知道以 ...
- pyDay15
内容来自廖雪峰的官方网站. 1.Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积. from functools ...
- kali linux 安装过程
kali linux 安装过程 获取镜像文件 首先需要去官网获取kali linux的镜像文件,本来获取了kali的最新版,由于有些方面还没有得到完善,与VM还没有完全兼容,所以换了视频上的1.0.8 ...
- 20155201 2016-2017-2 《Java程序设计》第三周学习总结
20155201 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 - 第四章要点: 4.1类与对象 类定义时使用class关键词,基本模式为 class na ...
- vim删除某一列
step1.按键盘上的Esc按键进入vi的命令行模式 step2.点击Ctrl和v按键 step3.按上下左右键选中要删除的内容 step4.按d即可删除选中的列
- 【Dubbo基础】dubbo学习过程、使用经验分享及实现原理简单介绍
一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一些非重要模块上,慢慢引入到一些稍微重要的功能上,半年时间,学习过程及线上使用遇到的些问题在此总结下. 整理这篇文章差不 ...
- Gym 101246D Fire in the Country(dfs求SG函数)
http://codeforces.com/gym/101246/problem/D 题意: 给定一个无向有环图,大火从1点开始,每个时间点与它相邻的点也将会着火,现在有两个人轮流操作机器人,机器人从 ...
- 前端工程化 - gulp
gulp是什么 gulp就是一个前端的自动化构建工具,在开发过程中很多重复的任务可以使用gulp和gulp插件自动完成.相比于grunt,gulp非常好上手,核心API只有4个,而且还有丰富的插件库. ...