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 ...
随机推荐
- composer update的错误使用以及如何更新composer.lock文件
用composer update装包是错误的. 安装包标准的方法应该是 require ,或者手动写 compose.json 文件,然后 composer install .如果只是需要更新 com ...
- rsync mac->windows openssh
rsync -azvP --progress -e "ssh -p 6666" /Users/codar/360\344\272\221\347\233\230/ghld/ rsy ...
- AFNetworking-2.5-源码阅读剖析--网络请求篇
一.前言 AFNetworking,非常友好简单的网络请求第三方框架,在GitHub中已经获得了25000++的star,链接地址:https://github.com/AFNetworking/AF ...
- 昨天的笔试题, StringBuffer
代码: public static void switchStr(StringBuffer x, StringBuffer y) { x.append(y); System.out.println(& ...
- oracle行转列练习
----------------------第一题--------------------------- create table STUDENT_SCORE ( name ), subject ), ...
- Freemarker 使用注意事项
上次借助 Freemaker 模板引擎来动态构造 Kylin 访问的 SQL,在使用过程中,遇到了一些坑. ${} 输出变量时需要注意: 示例 WHERE shop_id = ${val} 里的 v ...
- 关于c#分支语句和分支嵌套还有变量的作用域。
分支语句: if....else if....else 必须以 if 开头 后面加括号写入需要判断的内容. 举个栗子说明一下 if (bool类型(比较表达式)) // 他会判断括号内的条件是否 ...
- ENCODE:DNA 分子元件的百科全书
ENCODE(DNA分子元件的百科全书)是由国家人类基因研究所(NHGRI)资助的一个国际研究联盟, 该联盟的目标是:建立一份综合的人类基因组功能元件的清单,这些基本元件包括那些直接作用蛋白质和RNA ...
- p1627 [CQOI2009]中位数
传送门 分析 https://www.luogu.org/blog/user43145/solution-p1627 代码 #include<iostream> #include<c ...
- ZROI2018提高day5t2
传送门 分析 考场上傻了,写了个树剖还莫名weila...... 实际就是按顺序考虑每个点,然后从他往上找,一边走一边将走过的边染色,如果走到以前染过色的边就停下.对于每一个a[i]的答案就是之前走过 ...