Best Reward---hdu3613(manacher 回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613
题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是每个字母的价值之和,如果不是那么价值就是0;
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
abbadf
那么可以分成abba 和df abba的价值是1+2+2+1=6,df不是回文串所以价值为0;总价值就是6;
我们可以用数组L【i】R【i】来记录串的前缀长度为i的是否是回文串和后缀长度为i的是否是回文串;
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 1e6+; int v[], sum[N], p[N];
///sum[i]代表s串中前i个字符的价值总和;
///p[i]代表以i为中心的回文串的半径(包含i本身);
char s[N];
bool L[N], R[N];
///L[i]表示前i个字符是否是回文串,R[i]表示长度为i的后缀是否为回文串; void Manacher(char s[], int n)
{
int Id = , mx = ;
for(int i=; i<n; i++)
{
if(mx>i)
p[i] = min(mx-i, p[Id*-i]);
else
p[i] = ;
while(s[i+p[i]] == s[i-p[i]])
p[i]++;
if(mx < p[i]+i)
{
mx = p[i]+i;
Id = i;
} if(p[i] == i)
L[p[i]-] = true;
if(p[i]+i == n)
R[p[i]-] = true;
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(L, , sizeof(L));
memset(R, , sizeof(R));
memset(p, , sizeof(p));
memset(sum, , sizeof(sum));
for(int i=; i<; i++)
scanf("%d", &v[i]);
scanf("%s", s);
int len = strlen(s);
for(int i=; i<=len; i++)
sum[i] += sum[i-] + v[s[i-]-'a'];
for(int i=len; i>=; i--)
{
s[i+i+] = s[i];
s[i+i+] = '#';
}
s[]='$';
Manacher(s, *len+);
int ans = ;
for(int i=; i<len; i++)
{
int t=;
if(L[i])
t+=sum[i];
if(R[len-i])
t+=sum[len]-sum[i];
ans = max(ans, t);
}
printf("%d\n", ans);
}
return ;
}
Best Reward---hdu3613(manacher 回文串)的更多相关文章
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...
- UVA 12378 Ball Blasting Game 【Manacher回文串】
Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...
- HDU3068 最长回文 MANACHER+回文串
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- Manacher回文串算法学习记录
FROM: http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230 #include<stdio.h> #inc ...
- 【模板】Manacher 回文串
推荐两个讲得很好的博客: http://blog.sina.com.cn/s/blog_70811e1a01014esn.html https://segmentfault.com/a/1190000 ...
- HDU 3613 Best Reward(manacher求前、后缀回文串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题目大意: 题目大意就是将字符串s分成两部分子串,若子串是回文串则需计算价值,否则价值为0,求分 ...
- 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. ...
- (回文串 )Best Reward -- hdu -- 3613
http://acm.hdu.edu.cn/showproblem.php?pid=3613 Best Reward Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- jdom 读取
读取XML文档 读取文档,首先需要一个xml的解析器,它可以自动的解析出各个元素,并且把子元素作为自己的孩子节点,方便操作. 主要使用的函数: SAXBuilder.build("xxx.x ...
- DOM节点的三个属性
在文档对象模型 (DOM) 中,每个节点都是一个对象.DOM 节点有三个重要的属性 : 1. nodeName : 节点的名称 2. nodeValue :节点的值 3. nodeType :节点的类 ...
- PHP——内测:联系人管理
要求见文件-内测:联系人管理.pdf 数据库为mycontacts 表格为contacts,groups 表格内容为: zhuye.php <!DOCTYPE html PUBLIC " ...
- dp - HNU 13404 The Imp
The Imp Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13404&cour ...
- MapReduce 中的两表 join 几种方案简介
转自:http://my.oschina.net/leejun2005/blog/95186 MapSideJoin例子:http://my.oschina.net/leejun2005/blog/1 ...
- OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言,使页面更简洁;
OGNL(Object-Graph Navigation Language),可以方便地操作对象属性的开源表达式语言,使页面更简洁: 支持运算符(如+-*/),比普通的标志具有更高的自由度和更强的功能 ...
- 【BZOJ】1628 && 1683: [Usaco2007 Demo]City skyline 城市地平线(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1628 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 小结:STL
概要: c++的stl是个神奇的东西,需要好好学习. 技巧及注意: lower_bound是第一个大于等于要查找值 upper_bound是第一个大于要查找的值 stl中的容器中的比较几乎全都用< ...
- 数据库面试题之PL/SQL面试题
create table employee( id ) not null, -- 员工工号 salary ,) not null, -- 薪水 name ) not null -- 姓名 ); 第一题 ...
- 【NLP】新词发现
http://www.csdn.net/article/2013-05-08/2815186 http://blog.csdn.net/yuyu2223/article/details/7725705 ...