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) ...
随机推荐
- Integer类型的数据比较大小
因为实体类用的是Integer包装类,所以是对象,不能直接比较大小, 一.一个Integer一个Int可以直接比较大小 二.两个Integer需要用.intValue()方法比较大小: 例如:cw.g ...
- mr中间结果优化
转载请注明出处:http://blog.csdn.net/lastsweetop/article/details/9187721 作为输入 当压缩文件做为mapreduce的输入时,mapreduce ...
- 二分 + 模拟 - Carries
Carries Problem's Link Mean: 给你n个数,让你计算这n个数两两组合相加的和进位的次数. analyse: 脑洞题. 首先要知道:对于两个数的第k位相加会进位的条件是:a%( ...
- EasyUI Tree添加节点
创建foods tree首先,我们创建foods tree,代码像这样: <div style="width:200px;height:auto;border:1px solid #c ...
- JQuery EasyUI DataGrid动态合并(标题)单元) 一
JS: /** * EasyUI DataGrid根据字段动态合并单元格 * @param fldList 要合并table的id * @param fldList 要合并的列,用逗号分隔(例如:&q ...
- jQuery分页小插件
源码如下: $.fn.pager = function (pagerInfo) { var recordCount = this.size(); if (recordCount <= pager ...
- 关系运算符:instanceof
关系运算符:instanceof a instanceof Animal;(这个式子的结果是一个布尔表达式) a为对象变量,Animal是类名. 上面语句是判定a是否可以贴Animal标签.如果可以贴 ...
- Thinkphp整合各个功能
thinkphp整合Auth权限管理.支付宝.微信支付.阿里oss.友盟推送.融云即时通讯.云通讯短信.Email.Excel.PDF等等: 基于thinkphp扩展了大量的功能:而不改动thinkp ...
- 自定义控件_StickyNavLaout
关注我一.View结构原理1.extends linearLayout 继承想要用的布局,首先完成布局的填充在 onFinishInflate 方法中 findViewById(); @Overrid ...
- 说说M451例程讲解之串口
/**************************************************************************//** * @file main.c * @ve ...