Best Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4345    Accepted Submission(s): 1791

Problem Description
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him with honor and treasures for his great exploit.

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.

 
Input
The first line of input is a single integer T (1 ≤ T ≤ 10) - the number of test cases. The description of these test cases follows.

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.

 
Output
Output 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
 
Source
 
Recommend
lcy

题意:

给定一个字符串,以及每个字母拥有的价值。现在要把这个字符串切成两半,如果子串是回文,那么就可以加上这个子串的字符价值之和,如果不是回文这个子串的价值就是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】的更多相关文章

  1. 【manacher】HDU3068-最长回文

    [题目大意] 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. [manacher知识点] ①mx - i > P[j] 的时候,以S[j]为中心的回文子串 ...

  2. 【Manacher】Colorful String

    The value of a string s is equal to the number of different letters which appear in this string. You ...

  3. 【manacher】HDU4513-吉哥系列故事——完美队形II

    [题目大意] 求最长回文队伍且队伍由中间向两边递减. [思路] 和字符串一样的做法,在递推的时候增加判断条件:a[i-p[i]]<=a[i-p[i]+2]. #include<iostre ...

  4. BZOJ2160 拉拉队排练【Manacher】

    Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长的楚雨荨同学知道,帮助篮球队训 ...

  5. codeforces1045B Space Isaac 【manacher】【差分】

    题目大意: 题目是将$[0,m)$的数划成了两个集合,其中一个集合的元素个数不超过$n$.问在第一个集合中选出的数加上第二个集合中选出的数的和中没有出现的数有哪些. 题目分析: 很有意思的一道题.方便 ...

  6. BZOJ4755 [JSOI2016]扭动的回文串 【后缀数组】【manacher】

    题目分析: 我写了史上最丑的后缀数组,怎么办? 首先manacher一遍两个串,这样只用考虑第三问.用$作为间隔符拼接两个串,把第一个串翻转.枚举回文中心,取最长的回文串,对于剩下的部分利用LCP匹配 ...

  7. LOJ6387 [THUPC2018] 绿绿与串串 【manacher】

    题目分析: 比较简单,先跑一边manacher,然后对于回文部分可以碰到末尾的一定满足条件,否则向后转移. 代码: #include<bits/stdc++.h> using namesp ...

  8. hdu 4513 最长不下降回文序列【manacher】

    <题目链接> 吉哥又想出了一个新的完美队形游戏!  假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形 ...

  9. hdu 3068 最长回文【manacher】(模板题)

    <题目链接> 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如ab ...

随机推荐

  1. MongoDB 进程控制系列二:结束进程

    1:如果某个进程产生了异常,可以考虑将其kill掉 db.killOp(10417) db.killOp(10417/*opid*/) 等同于: db.$cmd.sys.killop.findOne( ...

  2. 如何免费的让网站启用https

    本文源自酷壳:如何免费的让网站启用HTTPS 今天,我把CoolShell变成https的安全访问了.我承认这件事有点晚了,因为之前的HTTP的问题也有网友告诉我,被国内的电信运营商在访问我的网站时加 ...

  3. IDEA使用笔记(四)——工具栏的显示隐藏切换

    这也是在玩快捷键的时候,自己试验出来的,觉得不常用但是一旦想用了可能一下不知道怎么弄,还需要找,不如记下来,起码能加深一下印象!

  4. ceph 的 bufferlist

    bufferlist是buffer::list的别名,其由来在 http://bean-li.github.io/bufferlist-in-ceph/ 中有非常详细的介绍 其p.p_off.off字 ...

  5. pandas数组(pandas Series)-(5)apply方法自定义函数

    有时候需要对 pandas Series 里的值进行一些操作,但是没有内置函数,这时候可以自己写一个函数,使用 pandas Series 的 apply 方法,可以对里面的每个值都调用这个函数,然后 ...

  6. 【GMT43智能液晶模块】例程六:WWDG看门狗实验——复位ARM

    实验原理: STM32内部包含窗口看门狗,通过看门狗可以监控程序运行,程序运行 错误时,未在规定时间喂狗,自动复位ARM.本实验通过UI界面中按钮按下 停止喂狗,制造程序运行错误,从而产生复位. 示例 ...

  7. svn常见错误解决

    Svn冲突导致锁住的解决方案:错误码:svn: E155037: Previous operation has not finished; run 'cleanup' if it was interr ...

  8. Java知多少(81)框架窗口基础

    窗口是GUI编程的基础,小应用程序或图形界面的应用程序的可视组件都放在窗口中,在GUI中,窗口是用户屏幕的一部分,起着在屏幕中一个小屏幕的作用.有以下三种窗口: Applet窗口:Applet类管理这 ...

  9. Linux软件安装中RPM与YUM 区别和联系

    .tc-nav-row{ height:38px; margin: 8px 15px; } .tc-nav-tab{ text-align: center; color:#333333; line-h ...

  10. vue返回上一页面如果没有上一页面返回首页

    methods: { back(){ if (window.history.length <= 1) { this.$router.push({path:'/'}) return false } ...