hdu3613 Best Reward【Manacher】
Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4345 Accepted Submission(s): 1791
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.
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.
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
6
题意:
给定一个字符串,以及每个字母拥有的价值。现在要把这个字符串切成两半,如果子串是回文,那么就可以加上这个子串的字符价值之和,如果不是回文这个子串的价值就是0.
现在要求两个子串相加的最大价值。
思路:
用Manacher我们可以处理出,以某个节点$i$为中心时的回文串长度, 即$p[i]-1$。
如果暴力枚举分割点,如果子串的中心的$p[i] - 1$正好是子串的长度,那么说明这个子串是一个回文串。
这里由于我们要算每个字符的价值之和,可以预处理前缀和。
对于偶数长度的串和奇数长度的串分别求对应的价值。
//#include<bits/stdc++>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h> #define LL long long
#define ull unsigned long long
#define inf 0x3f3f3f3f using namespace std; int t;
int val[];
const int maxlen = 5e5 + ;
char s[maxlen], s_new[maxlen * ];
int s_val[maxlen], p[maxlen * ]; int init()
{
int len = strlen(s + );
s_new[] = '$';
s_new[] = '#';
int j = ;
for(int i = ; i <= len; i++){
s_new[j++] = s[i];
s_new[j++] = '#';
}
s_new[j] = '\0';
return j;
} void manacher()
{
int len = init();
int id, mx = ; for(int i = ; i < len; i++){
if(i < mx)p[i] = min(p[ * id - i], mx - i);
else p[i] = ;
while(s_new[i - p[i]] == s_new[i + p[i]])p[i]++;
if(mx < i + p[i]){
id = i;
mx = i + p[i];
}
}
} int main()
{
scanf("%d", &t);
while(t--){
memset(p, , sizeof(p));
for(int i = ; i < ; i++){
scanf("%d", &val[i]);
}
scanf("%s", s + ); int len = strlen(s + );
for(int i = ; i <= len; i++){
s_val[i] = s_val[i - ] + val[s[i] - 'a'];
}
manacher();
// cout<<s_new<<endl;
// for(int i = 0; i <= len * 2; i++){
// cout<<p[i]<<" ";
// }
// cout<<endl; int ans = ;
for(int i = ; i < len; i++){
int tmp = ;
if(p[i + ] - == i){
if(i % ){
tmp += s_val[i / ] * + val[s[(i + ) / ] - 'a'];
}
else{
tmp += s_val[i / ] * ;
}
}
if(p[i + len + ] - == len - i){
if((len - i) % ){
tmp += (s_val[i + (len - i) / ] - s_val[i]) * + val[s[i + (len + - i) / ] - 'a'];
}
else{
tmp += (s_val[i + (len - i) / ] - s_val[i]) * ;
}
}
ans = max(ans, tmp);
}
printf("%d\n", ans);
} }
hdu3613 Best Reward【Manacher】的更多相关文章
- 【manacher】HDU3068-最长回文
[题目大意] 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. [manacher知识点] ①mx - i > P[j] 的时候,以S[j]为中心的回文子串 ...
- 【Manacher】Colorful String
The value of a string s is equal to the number of different letters which appear in this string. You ...
- 【manacher】HDU4513-吉哥系列故事——完美队形II
[题目大意] 求最长回文队伍且队伍由中间向两边递减. [思路] 和字符串一样的做法,在递推的时候增加判断条件:a[i-p[i]]<=a[i-p[i]+2]. #include<iostre ...
- BZOJ2160 拉拉队排练【Manacher】
Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮球队训 ...
- codeforces1045B Space Isaac 【manacher】【差分】
题目大意: 题目是将$[0,m)$的数划成了两个集合,其中一个集合的元素个数不超过$n$.问在第一个集合中选出的数加上第二个集合中选出的数的和中没有出现的数有哪些. 题目分析: 很有意思的一道题.方便 ...
- BZOJ4755 [JSOI2016]扭动的回文串 【后缀数组】【manacher】
题目分析: 我写了史上最丑的后缀数组,怎么办? 首先manacher一遍两个串,这样只用考虑第三问.用$作为间隔符拼接两个串,把第一个串翻转.枚举回文中心,取最长的回文串,对于剩下的部分利用LCP匹配 ...
- LOJ6387 [THUPC2018] 绿绿与串串 【manacher】
题目分析: 比较简单,先跑一边manacher,然后对于回文部分可以碰到末尾的一定满足条件,否则向后转移. 代码: #include<bits/stdc++.h> using namesp ...
- hdu 4513 最长不下降回文序列【manacher】
<题目链接> 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形 ...
- hdu 3068 最长回文【manacher】(模板题)
<题目链接> 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如ab ...
随机推荐
- Mac下用zsh
最近好多实验要跑,有时候Finder切换来切换去,感觉还不如用terminal. Mac默认的shell是bash.所以说我其实今天才弄明白shell和terminal之间的关系.在人和计算机内核之间 ...
- [AaronYang]那天有个小孩跟我说Js正则
按照自己的思路学习Node.Js 随心出发.突破正则冷门知识点,巧妙复习正则常用知识点 标签:AaronYang 茗洋 Node.Js 正则 Javascript 本篇博客地址:http://ww ...
- Service discovery
https://www.cnblogs.com/dirt2/p/5987067.html Use Assigned Numbers in the Service Discovery Protocol ...
- mark 阿里支付
开源软件企业版特惠高校版博客 我的码云 ·· 8月18日(周六)成都源创会火热报名中,四位一线行业大牛与你面对面,探讨区块链技术热潮下的冷思考. 开源项目 > WEB应用开发 > Web开 ...
- php中cal_days_in_month不可用时的替代方法(计算一个月的天数)
在计算某个月中的天数时,由于PHP编译时没有加上--enable-calendar选项,会导致cal_days_in_month方法不可用. 这时,如果不能更改服务器的编译设置,可以通过以下方法实现该 ...
- mac 安装memcached以及启动memcached
参考链接:https://blog.csdn.net/whereismatrix/article/details/50485570
- 第三部分:Android 应用程序接口指南---第二节:UI---第五章 设置(Settings)
第5章 设置(Settings) 应用程序通常包括允许用户修改应用程序的特性和行为的设置功能.例如,一些应用程序允许用户指定通知是否启用或指定多久使用云同步数据.如果你想要为你的应用程序提供设置,你应 ...
- [k8s]jenkins部署在k8s集群
$ cat jenkins-pvc.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: jenkins-pvc spec: ...
- Multiplication of numbers
Questin: There is an array A[N] of N numbers. You have to compose an array Output[N] such that Outpu ...
- 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-12底层驱动之液晶画点驱动
视频简介:该视频介绍iCore3应用开发平台中液晶驱动的方法. 源视频包下载地址:链接:http://pan.baidu.com/s/1qXQoOQo 密码:gvgo 银杏科技优酷视频发布区:http ...