hdu 3613 KMP算法扩展
Best Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 500 Accepted Submission(s): 201
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std; const int MAX=+;
char s1[MAX],s2[MAX];
int f[MAX],sum[MAX],val[];
bool per[MAX],pos[MAX]; void getFail(char *a,int len)
{
int i,j;
f[]=f[]=;
for(i=;i<len;i++)
{
j=f[i];
while(j && a[i]!=a[j]) j=f[j];
f[i+]=(a[i]==a[j]?j+:);
}
} int find(char *a,char *b,int len)
{
int i,j=;
for(i=;i<len;i++)
{
while(j && a[i]!=b[j]) j=f[j];
if(a[i]==b[j]) j++;
}
return j==len?f[j]:j;//必需要分成两部分,排除整个为回文串的情况
} int main()
{
int n,k,i,num,ans,len;
scanf("%d",&n);
while(n--)
{
for(i=;i<;i++)scanf("%d",val+i);
scanf("%s",s1);
len=strlen(s1);
for(i=;i<=len/;i++) s2[i]=s1[len-i-],s2[len-i-]=s1[i];
sum[]=val[s1[]-'a'];
for(i=;i<=len;i++) sum[i]=sum[i-]+val[s1[i-]-'a'];
getFail(s1,len);
k=find(s2,s1,len);//求s1最大前缀回文串位置
memset(per,false,sizeof(per));
while(k) per[k]=true,k=f[k];//标记所有前缀回文串
getFail(s2,len);
k=find(s1,s2,len);//求s1最大后缀回文串位置
memset(pos,false,sizeof(pos));
while(k) pos[k]=true,k=f[k];//标记所有后缀回文串
ans=-INF;
for(i=;i<len;++i)
{
num=;
if(per[i]) num+=sum[i];
if(pos[len-i])num+=sum[len]-sum[i];
if(num>ans)ans=num; }
printf("%d\n",ans);
}
return ;
}
hdu 3613 KMP算法扩展的更多相关文章
- hdu 4300 kmp算法扩展
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- hdu 1686 KMP算法
题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...
- HDU 3613 Best Reward(扩展KMP求前后缀回文串)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分割 ...
- HDU 2594 kmp算法变形
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- KMP算法模板&&扩展
很不错的学习链接:https://blog.csdn.net/v_july_v/article/details/7041827 具体思路就看上面的链接就行了,这里只放几个常用的模板 问题描述: 给出字 ...
- 扩展KMP --- HDU 3613 Best Reward
Best Reward Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...
- HDU 3613 Best Reward 正反两次扩展KMP
题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
随机推荐
- python之道08
1.有如下文件,a1.txt,里面的内容为: 某某是最好的学校, 全心全意为学生服务, 只为学生未来,不为牟利. 我说的都是真的.哈哈 分别完成以下的功能: a,将原文件全部读出来并打印. 答案 f ...
- Python基础篇 -- 集合
set集合 set 中的元素是不重复的,无序的 里面的元素必须是可hash的,(int str tuple bool) set 就是dict 类型的数据,但是不保存value 只保存 key set集 ...
- Rop实战之利用VirtualProtect绕过DEP
CVE-2011-0065 Firefox mChannel UAF漏洞 为了实现任意代码执行,需要在mChannel对象释放后,用可控数据“占坑”填充它,因此,可在onChannelRedirect ...
- java基础——冒泡排序
最近开始准备面试,所以将Java基础复习一遍,又学习了冒泡排序 冒泡排序的基本思想是,对相邻的元素进行两两比较,顺序相反则进行交换,这样,每一趟会将最小或最大的元素“浮”到顶端,最终达到完全有序 ja ...
- React动态import()
React动态import() react-router@v4代码分离,推荐的import().这里分享webpack配置和使用方法. 首先安装两个必须的包 cnpm i react-loadable ...
- Python爬虫系列-BeautifulSoup详解
安装 pip3 install beautifulsoup4 解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(markup,'html,parser') Pyth ...
- 如何用纯 CSS 创作一个小球反弹的动画
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/OwWROO 可交互视频 ...
- destoon ip接口失效修改 修改后偶尔会加载很慢
因为百度ip转换增加了密匙验证,所以导致之前的接口无法再转换ip地址的信息,修复方法如下:打开include\cloud.func.php文件搜索: function iplookup($ip) { ...
- tab key usage
QA:gvim编辑ascii文本时由于tabkey的default setting 不合适编写Verilog代码(比如一个tab 代表多少空格) ANS: 1.tab 的自动补齐有两种usage自动补 ...
- python GIL锁、进程池与线程池、同步异步
一.GIL全局解释器锁 全局解释器锁 在CPython中,全局解释器锁(GIL)是一个互斥锁,它可以防止多个本机线程同时执行Python代码.之所以需要这个锁,主要是因为CPython的内存管理不是线 ...