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 ...
随机推荐
- cocos进阶教程(5)CC_CALLBACK_X系列的使用技巧
CC_CALLBACK_1,CC_CALLBACK_2,CC_CALLBACK_3 这些都是std::bind的宏,数字1,2,3主要表示要占位的数量,也是将来传递参数的数量. // new call ...
- oauth2(转载http://www.rollosay.com/it/%E4%BD%BF%E7%94%A8OAuth-Server-PHP%E5%AE%9E%E7%8E%B0OAuth2%E6%9C%8D%E5%8A%A1)
http://www.rollosay.com/it/%E4%BD%BF%E7%94%A8OAuth-Server-PHP%E5%AE%9E%E7%8E%B0OAuth2%E6%9C%8D%E5%8A ...
- 文件下载—SSM框架文件下载
1.准备上传下载的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>common ...
- 【转】ViewPager 一屏显示多个子页面
一.概述 项目中遇到一个需求:ViewPager 一屏显示多个子页面.因为之前没有做过这样的界面,所以经历了些许小插曲,特以记之! 主要内容来自: http://blog.csdn.net/JM_be ...
- SQLite 自定义函数,聚合,排序规则
SQLite 自定义函数,聚合,排序规则 1.使用自定义函数, 聚合以及排序规则的基本方法是使用回调函数.这些注册的函数的生命周期只存在于应用程序中, 并不存储在数据库文件中, 因此需要在每个连接建立 ...
- 一个简单清晰的Redis操作类-php
<?php /** * redis处理的二次封装 * */ class Redis{ private $_redis; private $_config; public function __c ...
- 20145325张梓靖 实验二"Java面向对象程序设计"
20145325张梓靖 实验二"Java面向对象程序设计" 程序设计过程 实验内容 使用TDD的方式设计实现复数类 Complex 编写代码 设计实现复数类 Complex,复数类 ...
- Linux清除Windows密码
下载安装ntfs-3g 下载驱动让linux挂载windows磁盘 https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2017.3.23.tgz 安装 t ...
- 记录web api的request以及response(即写log)
https://www.cnblogs.com/felixnet/p/5689501.html https://blog.csdn.net/Vblegend_2013/article/details/ ...
- Same Tree,判断两个二叉树是不是相同的树,结构相同,每个节点的值相同
算法分析:这道题很简单,利用递归即可. public class SameTree { public boolean isSameTree(TreeNode p, TreeNode q) { if(p ...