[扩展KMP][HDU3613][Best Reward]
题意:
将一段字符串 分割成两个串
如果分割后的串为回文串,则该串的价值为所有字符的权值之和(字符的权值可能为负数),否则为0。
问如何分割,使得两个串权值之和最大
思路:
首先了解扩展kmp
扩展KMP:给出模板串A和子串B,长度分别为lenA和lenB,要求在线性时间内,对于每个A[i](0<=i<=lenA-1),求出A[i..lenA-1]与B的最长公共前缀长度,记为ex[i](或者说,ex[i]为满足A[i..i+z-1]==B[0..z-1]的最大的z值)。
根据上一篇文章我们可知,判断是否为回文串是复杂度的瓶颈,在这里我们可以用扩展KMP来解决。
分析:将原串s1反转得到s2,然后进行s1,s2扩展KMP匹配,得到extend,对于s1的前i个字符如果和s2的后i个字符相等即extend[len-i] == i则前i个字符为回文串,判断后len-i个字符是否是回文串用s2,s1进行扩展KMP即可
代码//非我写的
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;
const int MAX=500000+10;
char s1[MAX],s2[MAX];
int next[MAX],extend1[MAX],extend2[MAX];
int sum[MAX],val[27];
void get_next(char *a,int len){
int k=0,i=1;
next[0]=len;//可有可无,因为用不上
while(k+1<len && a[k] == a[k+1])++k;
next[1]=k;//这里预先算好next[1]是因为不能k=0,否则next[i-k]=next[i]不是已算好的
k=1;
while(++i<len){//和EKMP的过程一样
int maxr=k+next[k]-1;
next[i]=min(next[i-k],max(maxr-i+1,0));//这里是扩展KMP的精髓,即算法核心思想就是这
while(i+next[i]<len && a[next[i]] == a[i+next[i]])++next[i];
if(i+next[i]>k+next[k])k=i;
}
}
void EKMP(char *a,char *b,int *extend,int len){
get_next(a,len);
int k=0,i=0;
while(k<len && a[k] == b[k])++k;
extend[0]=k;
k=0;
while(++i<len){
int maxr=k+extend[k]-1;
extend[i]=min(next[i-k],max(maxr-i+1,0));//next[i-k]是a与b从i开始的可能已经匹配的长度
while(i+extend[i]<len && a[extend[i]] == b[i+extend[i]])++extend[i];//这里是扩展KMP的精髓,即算法核心思想就是这
if(i+extend[i]>k+extend[k])k=i;
}
}
int main(){
int n;
cin>>n;
while(n--){
for(int i=0;i<26;++i)cin>>val[i];
scanf("%s",s1);
int len=strlen(s1);
for(int i=1;i<=len;++i){
sum[i]=sum[i-1]+val[s1[i-1]-'a'];
s2[i-1]=s1[len-i];
}
EKMP(s1,s2,extend1,len);
EKMP(s2,s1,extend2,len);
int ans=0,temp=0;
for(int i=1;i<len;++i){
if(extend1[len-i] == i)temp+=sum[i];//表示前i个字符是回文串
if(extend2[i] == len-i)temp+=sum[len]-sum[i];//表示后len-i个字符为回文串
if(temp>ans)ans=temp;
temp=0;
}
cout<<ans<<endl;
}
return 0;
}
[扩展KMP][HDU3613][Best Reward]的更多相关文章
- 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 扩展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 ...
- 扩展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
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- S - Best Reward 扩展KMP
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- KMP 、扩展KMP、Manacher算法 总结
一. KMP 1 找字符串x是否存在于y串中,或者存在了几次 HDU1711 Number Sequence HDU1686 Oulipo HDU2087 剪花布条 2.求多个字符串的最长公共子串 P ...
- 扩展KMP题目
hdu4333 /* 题意:字符串s[0..n-1],每次把最后一个字符放到前面,求形成的字符串比最初串分别小,相同,大于的个数 因为是为了练习扩展KMP所以肯定是扩展KMP, 为了循环方便,在后面复 ...
随机推荐
- TabBarItem图片大小改变
在TabBarItem设计的时候不需要title只要image的时候,如何将image居中显示. tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, ...
- nodejs 批处理运行 app.js
1.直接执行run.bat文件 以下的内容为批处理文件run.bat中的内容,批处理命令中NODE_PATH为Node.js的安装路径. 使用express 生成的项目.app.js为 ...
- [HeadFirst-HTMLCSS学习笔记][第十三章表格]
表格 -table 块 tr 行 table row th 表头 table head td 表数据 table data; caption 表格标题 <table> <captio ...
- [core Java学习笔记][第一二三章基本语法]
基本语法 1 Java 简单的类型 1.1 一些常量 正无穷大 Double.POSITVE_INFINITY 负无穷大 Double.NEGATIVE_INFINITY 不存在 Double.NaN ...
- Linux下安装软件的错误
1. make configure GEN configure/bin/sh: 1: autoconf: not foundmake: *** [configure] Error 127 解决:sud ...
- class创建单击事件
$(function () { $(".search-button").click(function () { $(" ...
- 数据结构算法及应用——二叉树
一.二叉树性质 特性1 包含n (n> 0 )个元素的二叉树边数为n-1 特性2 二叉树的高度(height)或深度(depth)是指该二叉树的层数(有几层元素,而不是有层的元素间隔) 特性3 ...
- 【转】实战Nginx与PHP(FastCGI)的安装、配置与优化
原文连接:http://ixdba.blog.51cto.com/2895551/806622 原文作者:南非蚂蚁 转载注明以上信息 一.什么是 FastCGIFastCGI是一个可伸缩地.高速地在H ...
- Eratosthenes筛选法计算质数
<C和指针>第6章第4道编程题: 质数就是只能被1和本身整除的数.Eratosthenes筛选法是一种计算质数的有效方法.这个算法的第一步就是写下所有从2至某个上限之间的所有整数.在算法的 ...
- 十二、享元(Flyweight)模式--结构模式(Structural Pattern)
Flyweight在拳击比赛中指最轻量级,即"蝇量级",有些作者翻译为"羽量级".这里使用"享元 模式"更能反映模式的用意. 享元模式以共享 ...