hdu 5157(树状数组+Manacher)
Harry and magic string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 236 Accepted Submission(s): 118
got a string T, he wanted to know the number of T’s disjoint palindrome
substring pairs. A string is considered to be palindrome if and only if
it reads the same backward or forward. For two substrings of T:x=T[a1…b1],y=T[a2…b2](where a1 is the beginning index of x,b1 is the ending index of x. a2,b2 as the same of y), if both x and y are palindromes and b1<a2 or b2<a1 then we consider (x, y) to be a disjoint palindrome substring pair of T.
For
each test case, there is a string T in the first line, which is
composed by lowercase characters. The length of T is in the range of
[1,100000].
aaaa
15
For the first test case there are 4 palindrome substrings of T.
They are:
S1=T[0,0]
S2=T[0,2]
S3=T[1,1]
S4=T[2,2]
And there are 3 disjoint palindrome substring pairs.
They are:
(S1,S3) (S1,S4) (S3,S4).
So the answer is 3.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int N = ;
char s[*N],t[*N];
char str[*N];
int p[*N];
int c[*N];
LL l[*N];
void manacher(int n)
{
int id =,maxr=;
p[]=;
for(int i=; i<*n+; i++)
{
if(p[id]+id>i)
p[i]=min(p[*id-i],p[id]+id-i);
else p[i]=;
while(s[i-p[i]]==s[i+p[i]])
++p[i];
if(id+p[id]<i+p[i]) id=i;
if(maxr<p[i])
maxr=p[i];
}
}
int lowbit(int x)
{
return x&-x;
}
void update(int idx,int v)
{
for(int i=idx; i<=*N; i+=lowbit(i))
{
c[i]+=v;
}
}
int getsum(int idx)
{
int sum = ;
for(int i=idx; i>; i-=lowbit(i))
{
sum+=c[i];
}
return sum;
}
int main()
{
while(scanf("%s",str)!=EOF)
{
int n=strlen(str);
s[]='#';
for(int i=; i<=n; i++)
{
s[*i]='#';
s[*i-]=str[i-];
}
manacher(*n);
memset(c,,sizeof(c));
for(int i=; i<=*n; i++)
{
int ori;
if(i%==) ori = i/+;
else ori = (i+)/;
int ori1 = (i+p[i]-)/+;
if(ori1<=ori) continue;
update(ori,);
update(ori1,-);
}
memset(l,,sizeof(l));
for(int i=; i<=n; i++)
{
l[i] = l[i-]+getsum(i);
}
reverse(s,s+*n+);
manacher(*n);
memset(c,,sizeof(c));
for(int i=; i<=*n; i++)
{
int ori;
if(i%==) ori = i/+;
else ori = (i+)/;
int ori1 = (i+p[i]-)/+;
if(ori1<=ori) continue;
update(ori,);
update(ori1,-);
}
LL ans = ;
for(int i=; i<=n; i++)
{
ans += getsum(i)*l[n-i];
}
printf("%lld\n",ans);
}
return ;
}
hdu 5157(树状数组+Manacher)的更多相关文章
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 4777 树状数组+合数分解
Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2852 (树状数组+无序第K小)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...
- HDU 4911 (树状数组+逆序数)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...
- hdu 5792(树状数组,容斥) World is Exploding
hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...
- HDU 1934 树状数组 也可以用线段树
http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...
- 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...
- 【模板】HDU 1541 树状数组
http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...
- 牛客练习赛64 如果我让你查回文你还爱我吗 线段树 树状数组 manacher 计数 区间本质不同回文串个数
LINK:如果我让你查回文你还爱我吗 了解到了这个模板题. 果然我不会写2333... 考试的时候想到了一个非常辣鸡的 线段树合并+莫队的做法 过不了不再赘述. 当然也想到了manacher不过不太会 ...
随机推荐
- loj2100 「TJOI2015」线性代数
先推公式,推出个这,然后因为是 \(0/1\) 矩阵,选一个有损耗,两个一组有加成,就想到了最大权闭合子图,(飞行计划问题) #include <iostream> #include &l ...
- gVim的 设置
在.vimrc中添加以下代码后,重启vim即可实现按TAB产生4个空格:set ts=4 (注:ts是tabstop的缩写,设TAB宽4个空格)set expandtab TAB替换为空格::set ...
- 2016年后web开发趋势是什么?
2016 年后 Web开发趋势是什么 来源:yafeilee.me 发布时间:2016-05-06 阅读次数:1378 3 近二年的进展 前端发展日新月异, 甚至有一句戏言: "每六星期 ...
- 二分查找问题(Java版)
二分查找问题(Java版) 1.一般实现 package search; /** * @author lei 2011-8-17 */ public class BinarySearch ...
- 转载: keepalived工作原理和配置说明
转自:http://outofmemory.cn/wiki/keepalived-configuration keepalived是什么 keepalived是集群管理中保证集群高可用的一个服务软件, ...
- 随笔 —— 门徒 & 无限恐怖
门徒 忧思缠身,所为何物 不知何人,可免世俗 每每朝暮,心无释处 悲从中来,如泣如诉 仁者存世,满怀悲苦 逝者如斯,追还无路 上天无门,开怀捧腹 无路偏行,我行我素 无限恐怖 饥寒苦难谁知故,日日行路 ...
- 安装LoadRunner11报缺少vc2005_sp1_with_atl_fix_redist的错误
找到安装程序自带的lrunner\Chs\prerequisites\vc2005_sp1_redist,双击运行vcredist_x86.exe,再重新安装LoadRunner即可成功. 参考资料: ...
- 【转载】Unity3D研究院之IOS&Andoird使用Sharesdk遇到的坑
这两天研究了一下ShareSDK,说实话挺好用的,但是还是有点坑的地方.那么雨松MOMO写下博文记录一下来我遇到的坑,嘿嘿. 大部分内容它的文档上已经说的很清楚了. http://wiki.share ...
- 孤荷凌寒自学python第三十二天python的代码块中的异常的捕获
孤荷凌寒自学python第三十二天python的代码块中的异常的捕获 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天简单了解了Python的错误陷阱,了解到其与过去学过的其它语言非常类似 ...
- android 远程Service以及AIDL的跨进程通信
在Android中,Service是运行在主线程中的,如果在Service中处理一些耗时的操作,就会导致程序出现ANR. 但如果将本地的Service转换成一个远程的Service,就不会出现这样的问 ...