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. ...
随机推荐
- JS事件类型--1
滚轮事件其实就是一个mousewheel事件,这个事件跟踪鼠标滚轮,类似Mac的触屏版. 一.客户区坐标位置 鼠标事件都是在浏览器视口的特定位置上发生的.这个位置信息保存在事件对象的clientX和c ...
- iOS或Mac开发者应该记住的前缀
- 浅谈JavaScript中的正则表达式(适用初学者观看)
浅谈JavaScript中的正则表达式 1.什么是正则表达式(RegExp)? 官方定义: 正则表达式是一种特殊的字符串模式,用于匹配一组字符串,就好比用模具做产品,而正则就是这个模具,定义一种规则去 ...
- Factorialize a Number-freecodecamp算法题目
Factorialize a Number(计算一个整数的阶乘) 要求 给定一个整数,求其阶乘(用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积) 思路 确定乘的次数 用for循环进行累 ...
- Java常用的一些容器
转自:https://www.cnblogs.com/LipeiNet/p/5888513.html 前言:在java开发中我们肯定会大量的使用集合,在这里我将总结常见的集合类,每个集合类的优点和缺点 ...
- PHP RBAC权限控制,基于CI框架(版本3.1.9)
2018年11月7日更新:目前功能已做到事件级别权限控制,如:后台用户的添加操作.删除操作和保存操作等具体到事件级的操作方法有权限则展示相应的操作菜单,没权限则隐藏相应菜单或提示无权限到目前算是真正做 ...
- linux SVN安装及配置教程
1.环境centos6.4 2.安装svnyum -y install subversion 3.配置 建立版本库目录mkdir /www/svndata svnserve -d -r /www/sv ...
- 【mysql】【windows】MySQL 服务无法启动,服务没有报告任何错误,请键入 NET HELPMSG 3534 以获得更多的帮助。
成功安装以后,启动MySQL,输入: net start mysql 提示: ”MySQL 服务无法启动,服务没有报告任何错误,请键入 NET HELPMSG 3534 以获得更多的帮助.” 查了下, ...
- java的重载(overload) (2013-10-11-163 写的日志迁移
有以下几点: 1.方法名相同. 2.方法的 参数类型.个数.顺序至少有一项不同. 3.方法的返回类型可以不同. 4.方法的修饰符可以不同. 5.仅返回类型和修饰符不同也不可以构成重载. ----- ...
- logging记录了其他操作的问题
做atm作业的时候,记录转账操作的那个功能的文件里,同时也记录了增加账号和冻结账号的操作 2018-11-28 17:14:51,754 : transfer : INFO : 用户edward向用户 ...