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.

InputThe 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: 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的更多相关文章

  1. HDU 3613 Best Reward 正反两次扩展KMP

    题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...

  2. HDU - 3613 Best Reward(manacher或拓展kmp)

    传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...

  3. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

  4. HDU 3613 Best Reward(扩展KMP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...

  5. HDU 3613 Best Reward(扩展KMP求前后缀回文串)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...

  6. HDU 3613 Best Reward(manacher求前、后缀回文串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...

  7. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  8. HDU 3613 Best Reward(KMP算法求解一个串的前、后缀回文串标记数组)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  9. hdu 3613 Best Reward (manachar算法)

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Prob ...

随机推荐

  1. YTD易出现断层问题,请注意!

    declare @table table( company_id int ,--公司编号 quarter_num ),--季度 disti ),--分销商 num int --数量 ) insert ...

  2. CCPC2018-湖南全国邀请赛 Solution

    A - Easy $h$-index 后缀扫一下 #include <bits/stdc++.h> using namespace std; #define ll long long #d ...

  3. 前端学习笔记之CSS后代选择器、子元素选择器、相邻兄弟选择器区别与详解

    派生选择器用的很多,派生选择器具体包括为后代选择器.子元素选择器.相邻兄弟选择器,我们来理解一下他们之间的具体用法与区别. 1.css后代选择器语法:h1 em {color:red;} 表示的是从h ...

  4. Python Web学习笔记之为什么设计GIL

    GIL(global interpreter lock),全局解释器锁,是很多编程语言实现中都具有的特性,由于它的存在,解释器无法实现真正的并发.它也是 Python 中经常讨论的话题之一. Pyth ...

  5. ExtJS清除表格缓存

    背景 在使用ExtJS时遇到不少坑,如果不影响使用也无所谓,但是有些不能忍的,比如表格数据缓存问题.如果第一次打开页面查询出一些数据展示在表格中:第二次打开,即使不查询也会有数据,这是缓存的数据. 我 ...

  6. Knockout 模板使用

    html 代码: @using GreenWay.Models; @{ Model.Scripts = new string[] { "~/Scripts/paginationViewMod ...

  7. LightOJ 1027 A Dangerous Maze(期望)

    https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新 ...

  8. Yii框架(二)Model处理数据

    熟悉php的autoload机制,自己实现一个autoload函数 一.复习框架: basic/ 应用根目录 composer.json Composer 配置文件, 描述包信息 config/ 包含 ...

  9. flutter 安装详细教程

    Flutter 是 Google 用以帮助开发者在 iOS 和 Android 两个平台开发高质量原生 UI 的移动 SDK.Flutter 兼容现有的代码,免费且开源,在全球开发者中广泛被使用. 安 ...

  10. node中session的管理

    请看这个博客:   http://spartan1.iteye.com/blog/1729148 我自己的理解 session俗称会话. 第一次访问服务器的时候由服务器创建,相当于一个cookie(就 ...