Best Reward 拓展kmp
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.
For each test case, the first line is 26 integers: v1, v2, ..., v26 (-100 ≤ vi ≤ 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 v1, the value of 'b' is v2, ..., and so on. The length of the string is no more than 500000.
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
6
分析:
首先原始串为S,将S逆转得到串T.(S=abcaaa,那么T=aaacba).
S串的后缀回文:即S串中区间[i,n-1]的串是不是回文?
将S作为主串,T串用扩展KMP算法去匹配S,extend1[n]数组保存匹配结果.如果extend1[i]+i==n时(n为S的长),那么以S[i]为首字符一直到底n-1位置的串是回文串,否则不是.(自己举个例子验证一下)
S串的前缀回文:即S串中区间[0,i-1]的串是不是回文?
将T作为主串,S串用扩展KMP算法去匹配T,extend2[n]数组保存匹配结果.如果extend2[len-i]+len-i==n时(n为S的长),那么以S[i-1]为尾字符一直到0位置的串是回文串,否则不是.(自己举个例子验证一下)
仔细思考下上面的模型.
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std; void EKMP(char s[],char t[],int nex[],int extend[])//s为主串,t为模板串
{
int i,j,p,L;
int lens=strlen(s);
int lent=strlen(t);
nex[]=lent;
j=;
while(j+<lent && t[j]==t[j+])j++;
nex[]=j; int a=;
for(i=;i<lent;i++)
{
p=nex[a]+a-;
L=nex[i-a];
if(i+L<p+)nex[i]=L;
else
{
j=max(,p-i+);
while(i+j<lent && t[i+j]==t[j])j++;
nex[i]=j;
a=i;
}
} j=;
while(j<lens && j<lent && s[j]==t[j])j++;
extend[]=j;
a=;
for(i=;i<lens;i++)
{
p=extend[a]+a-;
L=nex[i-a];
if(L+i<p+)extend[i]=L;
else
{
j=max(,p-i+);
while(i+j<lens && j<lent && s[i+j]==t[j])j++;
extend[i]=j;
a=i;
}
}
} const int MAXN=;
char str1[MAXN],str2[MAXN];
int sum[MAXN];
int v[];
int nex[MAXN];
int extend1[MAXN],extend2[MAXN]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
for(int i=;i<;i++)
scanf("%d",&v[i]);
scanf("%s",str1);
int len=strlen(str1);
sum[]=;
for(int i=;i<len;i++)
{
sum[i+]=sum[i]+v[str1[i]-'a'];
str2[i]=str1[len--i];
}
str2[len]=;
EKMP(str2,str1,nex,extend1);
EKMP(str1,str2,nex,extend2);
int ans=-;
//需要保证分成两部分,所以i从1到len-1
for(int i=;i<len;i++)
{
int tmp=;
if(i+extend1[i]==len)
{
tmp+=sum[len-i];
}
int pos=len-i;
if(pos+extend2[pos]==len)
{
tmp+=sum[len]-sum[pos];
}
if(tmp>ans)ans=tmp;
}
printf("%d\n",ans);
}
return ;
}
Best Reward 拓展kmp的更多相关文章
- HDU 3613 Best Reward ( 拓展KMP求回文串 || Manacher )
题意 : 给个字符串S,要把S分成两段T1,T2,每个字母都有一个对应的价值,如果T1,T2是回文串,那么他们就会有一个价值,这个价值是这个串的所有字母价值之和,如果不是回文串,那么这串价值就为0.问 ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- HDU - 3613 Best Reward(manacher或拓展kmp)
传送门:HDU - 3613 题意:给出26个字母的价值,然后给你一个字符串,把它分成两个字符串,字符串是回文串才算价值,求价值最大是多少. 题解:这个题可以用马拉车,也可以用拓展kmp. ①Mana ...
- hdu-4300(kmp或者拓展kmp)
题意:乱七八糟说了一大堆,就是先给你一个长度26的字符串,对应了abcd....xyz,这是一个密码表.然后给你一个字符串,这个字符串是不完整的(完整的应该是前半部分是加密的,后半部分是解密了的),然 ...
- hdu-4763(kmp+拓展kmp)
题意:给你一个串,问你满足最大字串既是前后缀,也在字符串除去前后缀的位置中出现过: 思路:我用的是拓展kmp求的前后缀,只用kmp也能解,在字符串2/3的位置后开始遍历,如果用一个maxx保存前2/3 ...
- poj-2752(拓展kmp)
题意:求一个串所有的前后缀字串: 解题思路:kmp和拓展kmp都行,个人感觉拓展kmp更裸一点: 拓展kmp: #include<iostream> #include<algorit ...
- hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)
传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...
- 拓展KMP算法详解
拓展KMP解决的问题是给两个串S和T,长度分别是n和m,求S的每一个后缀子串与T的最长公共前缀分别是多少,记作extend数组,也就是说extend[i]表示S[i,n-1](i从0开始)和T的最长公 ...
- KMP&拓展KMP
KMP算法 说明 KMP算法是一种比较高效的字符串匹配算法,可以在线性时间内求出一个串在另一个串的所有匹配位置. 解析 详解KMP 设模板串是 \(pattern\) 令 \(next[i] = ma ...
随机推荐
- echo和重定向
命令: echo 作用: echo有重复的意思,会在终端中显示参数指定的文字,通常会和重定向联合使用 使用: echo 文字内容 例子: 在终端中显示hello echo hello 命令: > ...
- 定制起始url(scrapy_redis)
爬虫:(在这里不用配置start_url,直接可以取redis里面取start_url,可以多个) from scrapy_redis.spiders import RedisSpider # cla ...
- 执行原生SQL语句的方式
原生sql语句 cursor方法:from api.models import *from django.db import connection,connectionscursor=connecti ...
- 22)django-中间件
一:中间件介绍 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后, django会根据自己的规则在合适的时机执行中间件中相应的方法. 在dj ...
- 点9图 Android设计中如何切图.9.png
转载自:http://blog.csdn.net/buaaroid/article/details/51499516 本文主要介绍如何制作 切图.9.png(点9图),另一篇姊妹篇文章Android屏 ...
- CLOB数据类型截取SUBSTR_按开始位置偏移量
--DBMS_LOB.substr不加参数表示全部截取,负向截取待定 CREATE OR REPLACE FUNCTION CLOB_SUBSTR( V_CLOB CLOB, N_OFFSET NUM ...
- hive学习04-员工部门表综合案例
知识点: 格式转换:cast(xxx as int) 按某列分桶某列排序,排序后打标机:例如:求每个地区工资最高的那个人的信息: ROW_NUMBER() OVER(PARTITION BY COLU ...
- 来,了解一下Java内存模型(JMM)
网上有很多关于Java内存模型的文章,在<深入理解Java虚拟机>和<Java并发编程的艺术>等书中也都有关于这个知识点的介绍.但是,很多人读完之后还是搞不清楚,甚至有的人说自 ...
- linux 批量进行:解压缩某一类压缩文件类型的文件
1: 编写脚本 [oracle@oracle oracle]$ vim unzip.sh ziphome=/u01/app/oracle ziplist=`du -a $ziphome |grep ' ...
- Confluence 6 配置时间和日期格式
你可以修改你 Confluence 为用户显示的时期和时间格式.设置的句法使用的是 SimpleDateFormat class,请参考 Java SimpleDateFormat 文档中的内容来设置 ...