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 ...
随机推荐
- 10-10C#基础---数据类型之间的转换
10-10 C#基础数据类型转换(熟练掌握) 第一课 数据类型之间的转换 基本类型的转换:自动转换(隐式转换)和强制转换(显示转换) 装箱转换:允许值类型隐式转换成引用类型. 拆箱转换:允许将引用类 ...
- hbase.client.RetriesExhaustedException: Can't get the locations hive关联Hbase查询报错
特征1: hbase.client.RetriesExhaustedException: Can't get the locations 特征2: hbase日志报错如下:org.apache.zoo ...
- 【275】◀▶ Python 控制语句说明
参考:Python循环语句 01 for 循环语句. 02 while 循环语句. 03 if...else 选择语句. 04 continue 执行循环语句中的下一条循环. 05 ...
- Emulator 模拟器起不来
内存过大 打开SDK Manager.Avd Manager 新建 adb 命令不识别,因为环境变量里没有加入platform-tools文件夹 下载并按照下面这个更新,会帮助还原VS2012,我这 ...
- bootstrap媒体查询常用写法
@media (max-width: 768px) { /*超小屏幕设备 手机*/ } @media (min-width: 768px) and (max-width: 992px) { /*小屏幕 ...
- css知多少(10)——display(转)
css知多少(10)——display 1. 引言 网页的所有元素,除了“块”就是“流”,而且“流”都是包含在“块”里面的(最外层的body就是一个“块”).在本系列一开始讲<浏览器默认样式 ...
- Swing绘图机制
------------------siwuxie095 工程名:TestSwingPaintMethod 包名:com.siwuxie095.swin ...
- 项目一:在线下单(补充) activeMQ使用(重点) 重构客户注册功能,发短信功能分离
1 课程计划 1.在线下单(补充) 2.activeMQ使用(重点) n 简介和安装 n activeMQ入门案例 n spring整合activeMQ应用 3.重构客户注册功能,发短信功能分离 n ...
- C/C++读写csv文件
博客转载自:http://blog.csdn.net/u012234115/article/details/64465398 C++ 读写CSV文件,注意一下格式即可 #include <ios ...
- tr td th是什么的缩写
tr是 table row 表格的行 td是table data th是table heading表格标题 ,一般表格第一行的数据都是table heading