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读写文件实际操作的五大步骤
Python读写文件在计算机语言中被广泛的应用,如果你想了解其应用的程序,以下的文章会给你详细的介绍相关内容,会你在以后的学习的过程中有所帮助,下面我们就详细介绍其应用程序. 一.打开文件 Pytho ...
- 01_11_Strtus2简单数据验证
01_11_Strtus2简单数据验证 1. 引入struts标签 <%@taglib uri="/struts-tags" prefix="s" %&g ...
- Spring Security和Shiro的比较和使用
https://blog.csdn.net/it_java_shuai/article/details/78054951 Spring Security和Shiro的比较和使用 2017年09月21日 ...
- 使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)
使用ab( apache benchmark )测试的时候,使用如下命令: ab -n 15000 -c 200 http://localhost/abc/abc.php 执行操作一定条数,或连续 ...
- Unity基础-外部导入C# Dll(汇编集)
外部导入C# Dll(汇编集) 使用创建一个dll工程 添加依赖的dll 导入Unity中,放入Assets的任意文件夹中 使用代码生成的dll汇编集只要"use dll的名字"引 ...
- Unbuntu18.04如何备份
以后可能用的到:https://blog.csdn.net/qq_35523593/article/details/78545530
- 04Windows中的字符类型
1. Windows 中常用的数据类型定义 //WinNt.h中定义 typedef unsigned short wchar_t; //A 16-bit character typedef char ...
- Vue中引入TradingView制作K线图
**前言: 本文使用的是1.10版本 , 可通过TradingView.version()查看当前版本. 附上开发文档地址:https://zlq4863947.gitbooks.i...** 一.修 ...
- ise与win8兼容解决方案
win8中ise无法加载code,显示impact4.exe停止运行. 解决方法如下: 找到程序安装路径 1.进入文件夹 D:\Xilinx\14.6\ISE_DS\ISE\lib\nt64 把li ...
- apply(), applymap(), map()
Pandas 中map, applymap and apply的区别 https://blog.csdn.net/u010814042/article/details/76401133/ Panda ...