[Manacher][HDU3613][Best Reward]
题意:
将一段字符串 分割成两个串
如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0。
问如何分割,使得两个串权值之和最大
思路:
裸的:
枚举分割点,计算,O(n) 判断是否回文
总复杂度O(n^2)
优化:
利用Manacher的预处理 O(1)判断是否回文
复杂度O(n)
//Manacher
/*
** 求最长回文子串
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
const int MAXN=550000;
char Ma[MAXN*2];
int Mp[MAXN*2];
void Manacher(char s[],int len){
int l=0;
Ma[l++]='$';
Ma[l++]='#';
for(int i=0;i<len;i++){
Ma[l++]=s[i];
Ma[l++]='#';
}
Ma[l]=0;
int mx=0,id=0;
for(int i=0;i<l;i++){
Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]]) Mp[i]++;
if(i+Mp[i]>mx){
mx=i+Mp[i];
id=i;
}
}
}
/*
* abaaba
* i: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
*Ma[i]: $ # a # b # a # a # b # a #
*Mp[i]: 1 1 2 1 4 1 2 7 2 1 4 1 2 1
*/
char s[MAXN];
int sum[MAXN*2];
int v[30];
void input()
{
for(int i=1;i<=26;i++)
scanf("%d",&v[i]);
scanf("%s",s);
}
void get_sum()
{
int len=strlen(Ma);
for(int i=1;i<len;i++)
{
sum[i]=sum[i-1];
if('a'<=Ma[i]&&Ma[i]<='z')
sum[i]+=v[Ma[i]-'a'+1];
// printf("%d %d\n",i,sum[i]);
}
}
void solve()
{
int len=strlen(s);
Manacher(s,len);
get_sum();
len=strlen(Ma);
int ans=-0x3f3f3f3f;
for(int i=3;i<len-1;i=i+2)
{
int a1=0,a2=0;
//判断之前的串是不是回文串
if((((2+(i-1))/2)+Mp[(2+(i-1))/2]-1)>=(i-1))
a1=sum[i]-sum[1];
if(((len+i-1)/2-Mp[(len+i-1)/2]+1)<=(i+1))
a2=sum[len-2]-sum[i];
if(ans<a1+a2)
ans=a1+a2;
}
cout<<ans<<endl;
}
int main()
{
// freopen("a.in","r",stdin);
int T;
cin>>T;
while(T--)
{
input();
solve();
}
}
[Manacher][HDU3613][Best Reward]的更多相关文章
- HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举
题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) Memor ...
- hdu3613 Best Reward 扩展kmp or O(n)求最大回文子串
/** 题目:hdu3613 Best Reward 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意:有一个字符串,把他切成两部分. 如果这部 ...
- 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 ...
- hdu3613 Best Reward
先manacher.然后前缀和价值,枚举切点,O(1)判断切后是否回文 #include <iostream> #include <cstring> #include < ...
- [扩展KMP][HDU3613][Best Reward]
题意: 将一段字符串 分割成两个串 如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0. 问如何分割,使得两个串权值之和最大 思路: 首先了解扩展kmp 扩展K ...
- KMP 、扩展KMP、Manacher算法 总结
一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...
- HDU-3613-Best Reward(Manacher, 前缀和)
链接: https://vjudge.net/problem/HDU-3613 题意: After an uphill battle, General Li won a great victory. ...
随机推荐
- hdu 4908 BestCoder Sequence
# include <stdio.h> # include <algorithm> using namespace std; int main() { int n,m,i,su ...
- linux TIME_WAIT过多的解决方法
查看TCP状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'查看SOCKET状态:cat /proc/n ...
- 零拷贝概念 -- linux内核
零拷贝(zero-copy) 备快速网络接口的主要技术. 零拷贝技术通过降低或消除关键通信路径影响速率的操作,降低传输数据的操作系统开销和协议处理开销,从而有效提高通信性能,实现快速传输数据. 零拷贝 ...
- linux在文件打包和压缩
1. 打包和压缩文件 linux现在经常使用gzip和bzip2要压缩的文件.tar压缩文件. 经常使用的扩展: *.gz gzip压缩文件 *.bz2 bzip2压缩的文件 *.tar t ...
- apache访问控制设置
apache访问控制设置 (2009-03-17 11:24:36) 转载▼ 标签: it 杂谈 Order allow,deny 默认情况下禁止所有客户机访问 Order deny,all ...
- 如何搞定前端资源服务跨域问题之nginx篇
问题描述 1.首先让我们先看一张图 2.从图中,我们可以很清楚的看到当http请求的站点访问https的资源的时候会报出“Cross-Origin”跨域的问题.为什么会出现这样的错误,这是因为涉及到“ ...
- UISwitch 监听响应
UISwitch *swh = [[UISwitch alloc]initWithFrame:CGRectMake(100,100, 50, 30)]; swh.on = YES; ...
- Servlet开发(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- POJ Find The Multiple 1426 (搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22576 Accepted: 929 ...
- iOS Plist NSUserDefaults 数据存储
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...