HDU-3613-Best Reward(Manacher, 前缀和)
链接:
https://vjudge.net/problem/HDU-3613
题意:
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.
思路:
跑一边马拉车, 对马拉车修改后的字符串跑前缀和.
枚举切割点即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 5e5+10;
int hw[MAXN*2], val[30], Sum[MAXN*2];
char s[MAXN], ss[MAXN*2];
void Manacher(char *str)
{
int maxr = 0, mid;
int len = strlen(str);
for (int i = 1;i < len;i++)
{
if (i < maxr)
hw[i] = min(hw[mid*2-i], maxr-i);
else
hw[i] = 1;
while (str[i+hw[i]] == str[i-hw[i]])
hw[i]++;
if (hw[i]+i > maxr)
{
maxr = hw[i]+i;
mid = i;
}
}
}
void Change(char *str, char *to)
{
to[0] = to[1] = '#';
int len = strlen(str);
for (int i = 0;i < len;i++)
{
to[i*2+2] = str[i];
to[i*2+3] = '#';
}
to[len*2+2] = 0;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
for (int i = 0;i < 26;i++)
scanf("%d", &val[i]);
scanf("%s", s);
Change(s, ss);
Manacher(ss);
int len = strlen(ss);
Sum[0] = Sum[1] = 0;
for (int i = 2;i < len;i++)
{
if (ss[i] == '#')
Sum[i] = Sum[i-1];
else
Sum[i] = Sum[i-1]+val[ss[i]-'a'];
}
int ans = -1e8;
for (int i = 3;i <= len-2;i += 2)
{
int lmid = (i+1)/2, rmid = (len-1+i)/2;
int tmp = 0;
if (hw[lmid] == lmid)
tmp += Sum[i];
if (hw[rmid]+rmid == len)
tmp += Sum[len-1]-Sum[i];
ans = max(ans, tmp);
}
printf("%d\n", ans);
}
return 0;
}
HDU-3613-Best Reward(Manacher, 前缀和)的更多相关文章
- hdu 3613"Best Reward"(Manacher算法)
传送门 题意: 国王为了犒劳立下战功的大将军Li,决定奖给Li一串项链,这个项链一共包含26中珠子"a~z",每种珠子都有 相应的价值(-100~100),当某个项链可以构成回文时 ...
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU - 3613 Best Reward(manacher或拓展kmp)
传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- Best Reward HDU - 3613(马拉车+枚举+前缀和)
题意: 给你一串字符串,每个字符都有一个权值,要求把这个字符串在某点分开,使之成为两个单独的字符串 如果这两个子串某一个是回文串,则权值为那一个串所有的字符权值和 若不是回文串,则权值为0 解析: 先 ...
- HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )
题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- HDU 3613 Best Reward(扩展KMP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3613 [题目大意] 一个字符串的价值定义为,当它是一个回文串的时候,价值为每个字符的价值的和,如果 ...
- hdu 3613 Best Reward
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- Mysql创建、使用循环函数
创建函数 create procedure names() begin declare i int default 0; while i < 3000 do INSERT INTO studen ...
- 【LOJ】#3031. 「JOISC 2019 Day1」聚会
LOJ#3031. 「JOISC 2019 Day1」聚会 听说随机可过? 我想了很久想了一个不会被卡的做法,建出前\(u - 1\)个点的虚树,然后找第\(u\)个点的插入位置,就是每次找一条最长链 ...
- PC端判断屏幕宽度到达手机宽度的时候,直接跳转手机页面
<script> // //判断屏幕宽度到达手机宽度的时候,直接跳转手机页面 // window.addEventListener("resize", function ...
- python3.6以后的新写法
声明redis_store为StrictRedis 类型,值为None,用处:在别处调用时,如果redis_store仍为None,不会有提示(自动补全的提示),如果想要自动补全的提示则写成这样,函数 ...
- spark学习之Lambda架构日志分析流水线
单机运行 一.环境准备 Flume 1.6.0 Hadoop 2.6.0 Spark 1.6.0 Java version 1.8.0_73 Kafka 2.11-0.9.0.1 zookeeper ...
- Fix Scheduled Task Won’t Run for .BAT File
Step 1: Check File/Folder Permissions The first step to fixing this issue is ensuring that the accou ...
- JSON和AJAX基础
前一段时间做老师留的企业图谱作业,和查询功能都需要用到AJAX .然后做爬虫的时候发现好多网站都用到的是页面的局部刷新,也就是发送的AJAX请求.就去学了一下.简单总结 什么是 JSON ? JSON ...
- HTTP缓存总结
在具体了解 HTTP 缓存之前先来明确几个术语:1.缓存命中率:从缓存中得到数据的请求数与所有请求数的比率.理想状态是越高越好.2.过期内容:超过设置的有效时间,被标记为“陈旧”的内容.通常过期内容不 ...
- 在vue-cli项目中使用bootstrap
1.安装插件 npm install jquery --save npm install bootstrap --save npm install popper.js --save 2.配置webpa ...
- 2.OR Mapping 介绍
定义: ORM(Object Relational Mapping) -- 是一种为了解决面向对象与关系型数据库存在的互不匹配的现象的技术. 简单说:ORM是通过使用描述对象和数据库之间的映射的元数据 ...