kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)
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.
the number of test cases. The description of these test cases follows.
For each test case, the first line is 26 integers: v
1, v
2, ..., v
26 (-100 ≤ v
i ≤ 100, 1 ≤ i ≤ 26), represent the value of gemstones of each kind.
The second line of each test case is a string made up of charactor
'a' to 'z'. representing the necklace. Different charactor representing
different kinds of gemstones, and the value of 'a' is v
1, the value of 'b' is v
2, ..., and so on. The length of the string is no more than 500000.
OutputOutput a single Integer: the maximum value General Li can get from the necklace.Sample Input
2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
aba
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
acacac
Sample Output
1
6 必须包含两边,否则价值为0。 manacher:
思路:价值可以前缀和处理一下。然后manacher,枚举分割点,取价值最大
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=;
int p[maxn<<],T,sum[maxn],v[],pre[maxn],suf[maxn];
char s[maxn],snew[maxn<<]; int manacher(char* s) {
memset(pre,,sizeof(pre));
memset(suf,,sizeof(suf));
int l=,len=strlen(s);
snew[l++]='$';
snew[l++]='#';
for(int i=;s[i];i++) {
snew[l++]=s[i];
snew[l++]='#';
}
snew[l]=;
int id=,mx=;
for(int i=;i<l;i++) {
p[i]=mx>i?min(p[*id-i],mx-i):;
while(snew[i-p[i]]==snew[i+p[i]]) p[i]++;
if(i+p[i]>mx) {
mx=i+p[i];
id=i;
}
if(i-p[i]==) {
pre[p[i]-]=;//前缀
}
if(i+p[i]==l)
suf[len-(p[i]-)]=;//后缀
}
int maxx=-,tmp;
for(int i=;i<len-;i++) {//枚举分割点
tmp=;
if(pre[i]) {
tmp+=sum[i];
}
if(suf[i+]) {
tmp+=sum[len-]-sum[i];
}
maxx=max(tmp,maxx);
}
return maxx;
} int main() {
for(scanf("%d",&T);T;T--) {
for(int i=;i<;i++) scanf("%d",&v[i]);
scanf("%s",s);
sum[]=v[s[]-'a'];
for(int i=;s[i];i++) {
sum[i]=sum[i-]+v[s[i]-'a'];
}
printf("%d\n",manacher(s));
}
return ;
}
ekmp
将字符串S逆序,然后用S匹配T,T匹配S 如果i+extend[i]==len 说明 i~en-1 是回文串
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=;
char s[maxn],t[maxn];
int T,sum[maxn],v[],Nexts[maxn],Nextt[maxn],extends[maxn],extendt[maxn]; void prekmp(int len) {
Nexts[]=len;
int j=;
while(j+<len&&s[j]==s[j+]) j++;
Nexts[]=j;
int k=;
for(int i=;i<len;i++) {
int L=Nexts[i-k],p=k+Nexts[k]-;
if(i+L<p+) Nexts[i]=L;
else {
j=max(,p-i+);
while(i+j<len&&s[i]==s[i+j]) j++;
Nexts[i]=j;
k=i;
}
}
Nextt[]=len;
j=;
while(j+<len&&s[j]==s[j+]) j++;
Nextt[]=j;
k=;
for(int i=;i<len;i++) {
int L=Nextt[i-k],p=k+Nextt[k]-;
if(i+L<p+) Nextt[i]=L;
else {
j=max(,p-i+);
while(i+j<len&&s[i]==s[j+i]) j++;
Nextt[i]=j;
k=i;
}
}
} void ekmp(int len) {
prekmp(len);
int j=;
while(j<len&&s[j]==t[j]) j++;
extendt[]=j;
int k=;
for(int i=;i<len;i++) {
int L=Nexts[i-k],p=k+extendt[k]-;
if(i+L<p+) extendt[i]=L;
else {
j=max(,p-i+);
while(i+j<len&&t[i+j]==s[j]) j++;
extendt[i]=j;
k=i;
}
}
j=;
while(j<len&&s[j]==t[j]) j++;
extends[]=j;
k=;
for(int i=;i<len;i++) {
int L=Nextt[i-k],p=k+extends[k]-;
if(i+L<p+) extends[i]=L;
else {
j=max(,p-i+);
while(i+j<len&&s[i+j]==t[j]) j++;
extends[i]=j;
k=i;
}
}
} int main() {
for(scanf("%d",&T);T;T--) {
for(int i=;i<;i++) scanf("%d",&v[i]);
scanf("%s",s);
int len=strlen(s);
for(int i=;i<len;i++) {
t[i]=s[len-i-];
if(i==) sum[i]=v[s[i]-'a'];
else sum[i]=sum[i-]+v[s[i]-'a'];
}
t[len]=;
ekmp(len);
int maxx=-,tmp;
for(int i=;i<len;i++) {
tmp=;
int j=len-i;
if(j+extends[j]==len) tmp+=sum[len-]-sum[len-i-];
if(i+extendt[i]==len) tmp+=sum[len-i-];
maxx=max(maxx,tmp);
}
printf("%d\n",maxx);
}
return ;
}
kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
随机推荐
- java 多线程系列基础篇(四)之 synchronized关键字
1. synchronized原理 在java中,每一个对象有且仅有一个同步锁.这也意味着,同步锁是依赖于对象而存在.当我们调用某对象的synchronized方法时,就获取了该对象的同步锁.例如,s ...
- 12-01JavaScript事件(Events)
JS事件 1.js事件通常和函数结合来使用,这样可以通过发生的事件来驱动函数的执行,从而引起html出现不同的效果. 2.属性(当这些事件的属性发生时,会触发function{}的函数): 1)ona ...
- 解决springMVC文件上传报错: The current request is not a multipart request
转自:https://blog.csdn.net/HaHa_Sir/article/details/79131607 解决springMVC文件上传报错: The current request is ...
- C#如何拿到从http上返回JSON数据?
第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) 第四章:C#如何生成JSON字符串提交给接口 ...
- ie6-ie8不支持opacity,rgba解决方法
半透明部分设置样式:opacity:0.7在ie9/ie10/ff/chrome/opera/safari显示正常. 但是这样在ie6-ie8中是不支持的,需要加上下面这句话: filter: pro ...
- Android studio导入svn工程
Quick Start——> Check outproject from Version——> Subversion——> ‘+’加号 ——> 输入网址 ——> 注意选择 ...
- Android中同一个ImageView中根据状态显示不同图片
一般: if(条件1) { image.setBackground(R.id.xxx1); } else if (条件2) { image.setBackground(R.id.xxx2); } 实际 ...
- oracle --(一)数据块(data Block)
基本关系:数据库---表空间---数据段---分区---数据块 数据块(data Block)一.数据块Block是Oracle存储数据信息的最小单位.这里说的是Oracle环境下的最小单位.Orac ...
- FTP 命令 上传下载
ftp ftp [-v] [-n] [-i] [-d] [-g] [-s:filename] [-a] [-w:windowsize] [computer] 参数-v 禁止显示远程服务器响应.-n ...
- day17-jdbc 6.Connection介绍
package cn.itcast.jdbc; import com.mysql.jdbc.Connection; import java.sql.DriverManager; import java ...