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 ...
随机推荐
- 语法错误: 标识符“acosf”
1>e:\vs2010\vc\include\cmath(19): error C2061: 语法错误: 标识符“acosf” 1>e:\vs2010\vc\include\cmath(1 ...
- web.config中namespace的配置(针对页面中引用)
1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意以下) 2,可以将命名空间提到web.config配置文件中去,此 ...
- Solaris Tips: Repairing the Boot Archive (ZT)
http://www.seedsofgenius.net/solaris/solaris-tips-repairing-the-boot-archive 注意以下是系统盘非镜像情况下的操作,如果系统盘 ...
- 通过在Oracle子表外键上建立索引提高性能
根据我的经验,导致死锁的头号原因是外键未加索引(第二号原因是表上的位图索引遭到并发更新).在以下两种情况下,Oracle在修改父表后会对子表加一个全表锁: 1)如果更新了父表的主键(倘若遵循关系数据库 ...
- Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.()
转自:https://blog.csdn.net/l1028386804/article/details/65449355 ava.lang.NoSuchMethodException: [org.s ...
- Javascript面向对象(二):构造函数的继承
这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个" ...
- 运行shell脚本报/bin/bash^M: bad interpreter错误排查方法
今天遇到一个奇怪的问题,从一个服务器上down下来的脚本,在本地电脑做了点修改之后,上传到另外一台服务器上来执行,就报这个错误,问度娘,是编码格式的问题,windows把sh格式的编码改成dos格式的 ...
- Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
Bitmap bmp =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Paint paint = new ...
- C++知识点总结(5)
1.为何静态成员函数不能调用非静态成员函数 静态成员函数可以不需要类的实例就直接使用,非静态的成员函数很可能用到一些成员变量,而成员变量的创建和初始化是在创建了类的实例时在构造函数调用的时候才进行的. ...
- oracle行转列练习
----------------------第一题--------------------------- create table STUDENT_SCORE ( name ), subject ), ...