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. 各hbase版本对hadoop各版本的支持情况

    http://hbase.apache.org/book/configuration.html#basic.prerequisites   HBase-0.94.x HBase-0.98.x (Sup ...

  2. 让WebService支持Get请求

    在C#中,新建一个webservice,默认是post类型的.如果需要支持Get请求,需要对web.config文件进行配置 <system.web> <compilation de ...

  3. C语言定义共享全局变量

    好久没写C语言了,突然忘记怎么定义全局共享变量了,由于老项目的Code Base都是C的风格,其中又大量用了全局变量,只能跟着糊一坨shit上去了.没办法. 再共享全局变量的global_shared ...

  4. Emacs flycheck插件配置中遇到的若干问题

    工欲善其事必先利其器,一个高效的代码检查工具会大大提高我们的开发效率.flycheck是Emacs中常用的一个代码编译检查工具,本文记录配置它的时候遇到的一些问题以及解决方法. flycheck的基本 ...

  5. WWDC 2018:Swift 更新了什么?

    本文转载自:https://juejin.im/post/5b1cb5805188257d507be5d4所有权归原文所有 WWDC 2018 Session 401 What's New in Sw ...

  6. JS中 HTMLEncode和HTMLDecode

    <!--js伪编码解码--><script language="javascript" type="text/javascript">f ...

  7. String的split方法支持正则表达式

    String的split方法支持正则表达式: 1. 正则表达式\s表示匹配任何空白字符 2. +表示匹配一次或多次

  8. latex学习(四)tlmgr

    官网说明文档:https://tug.org/texlive/doc/tlmgr.html,2018版已经被冻结了,所以tlmgr也不会更新了,要等到下一个大的版本才能更新. 1.用tlmgr查看已经 ...

  9. Gradle 的下载安装配置以及创建第一个Gradle 项目

    1. 什么是Gradle? Gradle是一个开源的构建自动化工具,专注于灵活性和性能. Gradle构建脚本使用Groovy或Kotlin DSL编写. 阅读Gradle功能,了解Gradle的功能 ...

  10. Python访问MongoDB,并且转换成Dataframe

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/13 11:10 # @Author : baoshan # @Site ...