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

Problem Description
Harry
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.
 
Input
There are several cases.
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].
 
Output
For each test case, output one number in a line, indecates the answer.
 
Sample Input
aca
aaaa
 
Sample Output
3
15

Hint

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.

 
Source
 
 
题意:找到一个串中不相交的回文串的数量.
题解:官方题解:
先求出p[i],表示以i为回文中心的回文半径,manacher,sa,hash都可以。然后我们考虑以i为回文中心的时候,可以贡献出多少答案。我们 先考虑长度为p[i]的那个回文子串,它能贡献的答案,就是末尾在i-p[i]之前的回文子串数,那么长度为p[i-1]的,也是同样的道理。所以我们需 要求一个f[x],表示末尾不超过x的回文子串总共有多少个,把f[i-p[i]]到f[i-1]累加起来即为以i为中心的回文子串能贡献的答案。那我们 先统计,以x为结尾的回文子串有多少个,设为g[x]。来看以j为中心的回文子串,它的回文半径是p[j],那么从j到j+p[j]-1的这一段,都会被 贡献一个回文子串,也就是这一段的g[]会被贡献1,这里的处理方法很多,不细说。然后求一次前缀和,即可得到f[x]。
 
#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)的更多相关文章

  1. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  4. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  5. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  6. HDU 1934 树状数组 也可以用线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...

  7. 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 ...

  8. 【模板】HDU 1541 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...

  9. 牛客练习赛64 如果我让你查回文你还爱我吗 线段树 树状数组 manacher 计数 区间本质不同回文串个数

    LINK:如果我让你查回文你还爱我吗 了解到了这个模板题. 果然我不会写2333... 考试的时候想到了一个非常辣鸡的 线段树合并+莫队的做法 过不了不再赘述. 当然也想到了manacher不过不太会 ...

随机推荐

  1. WCF入门一[WCF概述]

    一.什么是WCF WCF是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的.安全.可信赖.事务性的解决方案,且能与已有系统兼容协作 ...

  2. ST表学习

    啊谈不上学习了.复习一下原理留一下板子. $f\left[i,j \right]$表示以$i$为起点,区间长度为${2}^{j}$的区间最值.以最小值为例,即 $min\left(a\left [ k ...

  3. HDU 4405 Aeroplane chess(期望dp)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. 如何让button保持点击状态

    http://blog.csdn.net/u010957508/article/details/38385207 他的原理就是在代码里面: view.setSelected(true); 而其他的: ...

  5. Java从数据库读取页面树形菜单

    从数据库加载菜单及子菜单主要使用递归的方法,具体实现可看代码 首先封装一个菜单pojo public class Menu { // 菜单id private String id; // 菜单名称 p ...

  6. android 管理Touch事件

    The onInterceptTouchEvent() method gives a parent the chance to see any touch event before its child ...

  7. 如何在Apache中使用PHP处理PHP文件

    一.将PHP预处理器作为Apache的模块(插件) Apache软件自身的功能都是基于模块化管理的. 将PHP预处理器作为Apache的一个模块即可. 在apache/conf/httpd.conf的 ...

  8. 在iis上部署asp.net mvc2.0

    mvc2.0是vs2010自带的,在开发环境下可以直接部署在iis中.在生产环境下,如果不能找到正确的mvc2.0版本,可以直接把开发环境下的System.Web.Mvc.dll拷贝过去使用. 1,  ...

  9. Percona-Tookit工具包之pt-heartbeat

      Preface       Replication delay is a common issue in MySQL replications.Especially in those replic ...

  10. Jmeter 场景设计

    今天的业务场景是: 1.管理员登录后台---登录成功后添加一个某类型的产品---产品添加成功后,再为该产品添加10个排期. 2.管理员登录后台--登录成功后添加多个不同类型产品---产品全部添加完成后 ...