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 ...
随机推荐
- C Primer Plus学习笔记(七)- 字符输入/输出和输入验证
单字符 I/O:getchar() 和 putchar() getchar() 和 putchar() 每次只处理一个字符 getchar() 和 putchar() 都不是真正的函数,它们被定义为供 ...
- import javax.servlet 出错(真的很管用)
Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...
- elasticsearch(3) curl命令
curl 操作http的get/post/put/delete CURL 命令参数-a/--append 上传文件时,附加到目标文件-A/--user-agent <string> 设置用 ...
- 监控和安全运维 1.6 nagios监控客户端-2
6. 继续添加服务服务端 vim /etc/nagios/objects/commands.cfg 增加: define command{ command_name check_nrpe comman ...
- Samba服务学习报错总结
1 2 3 4 5 此文献来至百度文库 http://wenku.baidu.com/link?url=hkHembjXcjoYRU9ky34a46Lzv5SAEutwa0v1_F8INQsdg_KK ...
- xcode减小静态库的大小(转)
减小静态库的大小 编译生成的.a文件太大,但又没有冗余的文件可以删除已减少体积,找了很久才找到解决办法,如下: Build Settings-->Generate Debug Symbols 将 ...
- LookupError: unknown encoding: cp65001解决办法
一.之前手上做的一个web项目,漏洞频发,服务器用的是菜鸟云服务器,那个应急响应中心不错,想不到乌云倒了,白帽子竟然被阿里系养了,题外话了,首先感谢白帽子提的漏洞,同时也感慨自己安全知识,以及意识的薄 ...
- Spring事务SPI及配置介绍
Spring事务SPI及配置介绍 标签: spring事务aop数据管理 2015-05-17 11:42 606人阅读 评论(0) 收藏 举报 分类: Spring(12) 版权声明:本文为 ...
- [Python Study Notes]堆叠柱状图绘制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- 基于cookie实现用户验证
#!/usr/bin/env python import tornado.ioloop import tornado.web class IndexHander(tornado.web.Request ...