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.

题意:给出一个由26种字母组成的字符串,每种字母有权值,当一个串是回文串的时候,它的权值是其中所有字母的权值之和,否则是0。

现在要将这个字符串切成两段,问两段的权值和最大是多少。

用前缀和处理串的前缀权值和。

manacher进行回文串的匹配处理。

贪心切分位置并用manacher处理的数组判断是否是回文子串,并用前缀和数组求值,取最大值。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=5e5+;
char s[maxn],t[maxn<<];
int p[maxn<<];
int a[maxn],v[]; void manacher(){
int len=strlen(s),l=;
t[l++]='$';
t[l++]='#';
for(int i=;i<len;++i){
t[l++]=s[i];
t[l++]='#';
}
t[l]=;
int maxx=,num=;
for(int i=;i<l;++i){
p[i]=maxx>i?min(p[*num-i],maxx-i):;
while(t[i+p[i]]==t[i-p[i]])p[i]++;
if(i+p[i]>maxx){
maxx=i+p[i];
num=i;
}
}
} int main(){
int T;
scanf("%d",&T);
while(T--){
for(int i=;i<;++i)scanf("%d",&v[i]);
scanf("%s",s);
int l=strlen(s);
a[]=v[s[]-'a'];
for(int i=;i<l;++i){
a[i]=a[i-]+v[s[i]-'a'];
}
manacher();
int ans=;
for(int i=;i<l-;++i){
int tmp=;
int num=p[i+]-;
if(num==i+)tmp+=a[i];
num=p[i+l+]-;
if(num==l-i-)tmp+=a[l-]-a[i];
if(tmp>ans)ans=tmp;
}
printf("%d\n",ans);
}
return ;
}

hdu3613 Best Reward manacher+贪心+前缀和的更多相关文章

  1. HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举

    题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  2. hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串

    /** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...

  3. 【贪心+前缀】C. Fountains

    http://codeforces.com/contest/799/problem/C [题意] 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 ...

  4. kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

    After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...

  5. HDU-3613-Best Reward(Manacher, 前缀和)

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

  6. hdu3613 Best Reward【Manacher】

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

  7. [Manacher][HDU3613][Best Reward]

    题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 裸的: 枚举分割点,计算, ...

  8. BZOJ3790神奇项链——manacher+贪心

    题目描述 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H 购买了两个机器.第一个机器可以生成所有形式 ...

  9. 【BZOJ3790】神奇项链 Manacher+贪心

    [BZOJ3790]神奇项链 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H ...

随机推荐

  1. am335x system upgrade uboot nand boot(三)

    在uboot 下初始化nand,一般需要做如下工作: 第一: 配置默认从NAND boot Index: include/configs/am335x_evm.h=================== ...

  2. 使用laravel搭建CURD后台页面

    配置即一切 一切皆于需求,后台从0开始搭建,但是写了一两个页面后发现太多的是对单表的增删改查操作,于是就想到了,能不能做一个快速搭建的后台.想到一句话,配置即一切.如果一个CURD后台能只进行配置就自 ...

  3. wordpress有用的插件

    代码高亮 https://wordpress.org/plugins/crayon-syntax-highlighter/ 这个貌似比较好用 百度UEditor(富文本编辑器) https://www ...

  4. 深度学习----Xavier初始化方法

    “Xavier”初始化方法是一种很有效的神经网络初始化方法,方法来源于2010年的一篇论文<Understanding the difficulty of training deep feedf ...

  5. 查找xml中的接口名及涉及表名并输出

    #! /usr/bin/env python3 # -*- coding:utf-8 -*- import xml.dom.minidom  #该模块被用来处理xml文件 import re #正则表 ...

  6. :适配器模式:Adapter

    #ifndef __ADAPTER_H__ #define __ADAPTER_H__ #include <iostream> using namespace std; class Duc ...

  7. Cracking The Coding Interview 1.1

    //原文: // // Implement an algorithm to determine if a string has all unique characters. What if you c ...

  8. thymeleaf之下拉框回显选中

    #1.select下拉框取值    <div class="form-group ">        <label id="resource" ...

  9. Centos7安装RabbitMQ解决Erlang依赖报错

    通过yum等软件仓库都可以直接安装RabbitMQ,但版本一般都较为保守. RabbitMQ官网提供了新版的rpm包(http://www.rabbitmq.com/download.html),但是 ...

  10. 一次练习 发现的问题,malloc free 无效;findfirstfile失败,writefile失败的原因

    #include <windows.h> #include <stdio.h> #include <shlwapi.h> #pragma comment(lib,& ...