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 ...
随机推荐
- 【软件安装】Xshell + XFtp
[问题]xshell evaluation period has expired 今天发现一个xshell过期的事情,其实官方提供对应的校园版本供大家使用 进入官方下载地址:xshell地址 填写个人 ...
- 2018-2019-1 20189215《Linux内核原理与分析》第五周作业
<庖丁解牛>第四章书本知识总结 系统调用的三层机制 API(应用程序编程接口) 中断向量(系统调用处理入口) 服务程序(系统调用内核处理系统) 计算机的硬件资源是有限的,为了减少有限资源的 ...
- usb_control_msg() -- 从设备读取各种信息
et_port_status() --> usb_control_msg()usb_get_descriptor() --> usb_control_msg()/usr/src/linux ...
- [Microsoft][ODBC Microsoft Access Driver] 参数不足,期待是 1
真tm坑. QString execStr = QString("SELECT * FROM [Log] WHERE [username]=\"yyy\" ") ...
- ubuntu18.04下挂载网络文件系统失败【学习笔记】
作者:庄泽彬(欢迎转载,请注明作者) PC: ubuntu18.04 说明: 之前ubuntu16.04下搭建的环境,开发板挂载网络文件系统是ok的,但是换到ubuntu18.04在启动的时候 ...
- git如何在自动生成补丁时指定补丁名的起始编号
答:使用选项--start-number,用法如下: git format-patch 1f43be --start-number=2 这样就可以生成起始编号为2的补丁名,类似0002-me.patc ...
- noip 2018 D1T3 赛道修建
noip 2018 D1T3 赛道修建 首先考虑二分答案,这时需要的就是对于一个长度求出能在树中选出来的最多的路径条数.考虑到一条路径是由一条向上的路径与一条向下的路径构成,或者仅仅是向上或向下的路径 ...
- [笔记] SQL性能优化 - 常用语句(一)
第一步 DBCC DROPCLEANBUFFERS 清除缓冲区 DBCC FREEPROCCACHE 删除计划高速缓存中的元素 从缓冲池中删除所有清除缓冲区.要求具有 sysadmin 固定服务器角色 ...
- springboot2 统一返回结果
统一返回结果是说,不用在controller层,new一个对象,或用工厂创建这个对象,再返回这个对象,而是这个Action该返回什么就返回什么, 我们再通过mvc的流程,再对返回对象做进一步的封装,以 ...
- pybedtools --bedtools的python包
http://daler.github.io/pybedtools/ 用个下面这个 >>> fn = pybedtools.example_filename('test.fa') & ...