hdu3613 Best Reward
先manacher。然后前缀和价值,枚举切点,O(1)判断切后是否回文
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int T, val[1000005], p[1000005], len, w[255];
char a[1000005];
void manacher(){
int mx=0, id=0;
for(int i=1; i<len; i++){
if(i<mx) p[i] = min(p[2*id-i], mx-i);
else p[i] = 1;
while(a[i-p[i]]==a[i+p[i]]) p[i]++;
if(mx<i+p[i]) mx = i + p[i], id = i;
}
}
int main(){
cin>>T;
w['#'] = w['$'] = 0;
while(T--){
for(int i=0; i<26; i++)
scanf("%d", &w[i+'a']);
scanf("%s", a);
len = strlen(a);
for(int i=len; i>=0; i--){
a[2*i+2] = a[i];
a[2*i+1] = '#';
}
len = 2 * len + 1;
a[0] = '$';
manacher();
for(int i=1; i<len; i++)
val[i] = val[i-1] + w[(int)a[i]];
int maxans=0;
for(int i=3; i<len; i+=2){
int tmp=0;
if((i+1)/2+p[(i+1)/2]>i) tmp += val[i];
if((i+len)/2+p[(i+len)/2]>len) tmp += val[len-1] - val[i-1];
maxans = max(maxans, tmp);
}
printf("%d\n", maxans);
}
return 0;
}
hdu3613 Best Reward的更多相关文章
- hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串
/** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...
- HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举
题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) Memor ...
- hdu3613 Best Reward【Manacher】
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu3613 Best Reward manacher+贪心+前缀和
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
- 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 ...
- [扩展KMP][HDU3613][Best Reward]
题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 首先了解扩展kmp 扩展K ...
- [Manacher][HDU3613][Best Reward]
题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 裸的: 枚举分割点,计算, ...
- KMP 、扩展KMP、Manacher算法 总结
一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
随机推荐
- JDBC和分包
JDBC(Java Data Base Connectivity,java数据库连接) JDBC是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写 ...
- All boundaries are conventions, waiting to be transcended.
All boundaries are conventions, waiting to be transcended.所有界限都是陈规,等着被打破.
- 机器学习&深度学习资料分享
感谢:https://github.com/ty4z2008/Qix/blob/master/dl.md <Brief History of Machine Learning> 介绍:这是 ...
- ABAP system landscape和vue项目webpack构建的最佳实践
基于Netweaver的ABAP transport route一般都有dev,test和prod三种类型的系统. 而Vue前端项目的webpack build设置也类似. 以SAP成都研究院数字创新 ...
- 删除表中一个字段的SQL语句
1.删除没有默认值的列:alter table Test drop COLUMN BazaarType 2.删除有默认值的列:先删除约束(默认值)alter table Test DROP CONST ...
- oracle的clob转换varchar2
time: 2008/02/29 author: skate oracle的clob转换varchar2 今天在做一个表的数据转移的时候,发现要他通过比较clob字段,但大家都知道clob字段是无法比 ...
- 分类回归树(CART)
概要 本部分介绍 CART,是一种非常重要的机器学习算法. 基本原理 CART 全称为 Classification And Regression Trees,即分类回归树.顾名思义,该算法既 ...
- JSONPath - XPath for JSON
http://goessner.net/articles/JsonPath/ [edit] [comment] [remove] |2007-02-21| e1 # JSONPath - XPath ...
- linux程序安装及包管理
程序包的封装类型: RPM软件包:扩展名为“.rpm”,使用rpm命令安装. DEB软件包:扩展名为“.deb”,使用DPKG包管理器. 源代码软件安装:程序员开发完成的原始代码,一般制作成“.tar ...
- js判断是否为app
var ua = navigator.userAgent; var isapp = ua.match("lenovomallapp") == null ? 0 : 1;