【HDU 3613】Best Reward
【题目链接】
【算法】
正反两遍EXKMP,即可
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXC 26
#define MAXL 500010 int T,ans,tmp,i,len;
int a[MAXC+],Next[MAXL],extend1[MAXL],extend2[MAXL],sum[MAXL];
char s1[MAXL],s2[MAXL]; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
}
inline void exkmp(char *s1,char *s2,int *Next,int *Extend) {
int i=,j,pos;
while (i + < len && s2[i+] == s2[i]) i++;
Next[] = i;
pos = ;
for (i = ; i < len; i++) {
if (i + Next[i-pos] < pos + Next[pos]) Next[i] = Next[i-pos];
else {
j = pos + Next[i-pos] - i;
if (j < ) j = ;
while (i + j < len && s2[j] == s2[i+j]) j++;
Next[i] = j;
pos = i;
}
}
i = ;
while (i < len && s1[i] == s2[i]) i++;
Extend[] = i;
pos = ;
for (i = ; i < len; i++) {
if (i + Next[i-pos] < pos + Extend[pos]) Extend[i] = Next[i-pos];
else {
j = pos + Extend[pos] - i;
if (j < ) j = ;
while (i + j < len && s1[i+j] == s2[j]) j++;
Extend[i] = j;
pos = i;
}
}
} int main() { read(T);
while (T--) {
for (i = ; i <= MAXC; i++) read(a[i]);
scanf("%s",s1);
len = strlen(s1);
sum[] = a[s1[]-'a'+];
for (i = ; i < len; i++) {
s2[len-i-] = s1[i];
if (i > ) sum[i] = sum[i-] + a[s1[i]-'a'+];
}
exkmp(s2,s1,Next,extend1);
exkmp(s1,s2,Next,extend2);
ans = ;
for (i = ; i < len - ; i++) {
tmp = ;
if (extend1[len-i-] == i + ) tmp += sum[i];
if (extend2[i+] == len - i - ) tmp += sum[len-] - sum[i];
ans = max(ans,tmp);
}
writeln(ans);
} return ; }
【HDU 3613】Best Reward的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- 【HDU 4699】 Editor
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- Jetson TK1 二:usb无线网卡的使用
一.总体是按照群里的文档“TK1连接无线网络”的步骤操作的,但也遇到了一些问题,如下: 1.自动配置设备并下载内核源代码到指定的目录下时(估计是解压时),出现时间超前之类的问题,原因是当前本地时间是几 ...
- Maven错误 diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)问题解决
如果在Maven构建时出现: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable d ...
- Python基础语法08--MySql应用
python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. DB-API 是一个规范. 它定义了一系列 ...
- 转: DNS 原理入门 (from 阮一峰)
转自:http://www.ruanyifeng.com/blog/2016/06/dns.html DNS 原理入门 作者: 阮一峰 日期: 2016年6月16日 DNS 是互联网核心协议之一. ...
- UVa 10295 - Hay Points
题目:有非常多工人.相应一个能力描写叙述表,每种能力有一个权值,求每一个工人的能力值. 分析:字符串.hash表,字典树.利用散列表或者字典树存储相应的单词和权值.查询就可以. 说明:注意初始化,计算 ...
- [Testing] Static Analysis Testing JavaScript Applications
The static code analysis and linting tool ESLint is the de-facto standard for linting JavaScript pro ...
- 用Perl发送邮件小例子
据传,Perl发送邮件有很多方案,但我只会用Mail::Sender这种方式,也就只能简单谈谈这种方式. 在参考众多网页后,程序书写如下: #!/usr/bin/perl -w use Mail::S ...
- 跟着实例学习设计模式(7)-原型模式prototype(创建型)
原型模式是创建型模式. 设计意图:用原型实例指定创建对象的类型,并通过拷贝这个原型来创建新的对象. 我们使用构建简历的样例的类图来说明原型模式. 类图: 原型模式主要用于对象的复制.它的核心是就是类图 ...
- C#语言 函数
- webform的操作完之后返回主页面的行定位
1.在repeater表格的行绑定时给行一个id(唯一id),此地方为绑定该表格的主键. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDA3OD ...