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不过不太会 ...
随机推荐
- textView代码设置文字居中失效 textView设置文字居中两种方法
1.TextView的高度占据整个父控件的高度,然后设置TextView的Grayvity Center就可以了. 2.如果第一个方法不行,那么,textView的高度设置为warp_content, ...
- 1.bootstrap的HTML文件编写规范
1.head标签里面的内容 <!DOCTYPE html> <html lang="zh-cn"> <head> <!-- 页面编码 -- ...
- Hyper-V中的Linux无法配置网络地址的解决办法
一周碰到2次在Hyper-V 2012中安装了Linux,也安装了IC 3.4.但是却无法配置IP地址的问题.因此造成很多不便,因此找机会把这个原因和解决办法进行了尝试. 这过程中感谢同事的提示,让我 ...
- 一道关于C++ 继承/虚函数 笔试题 [转]
转自:http://www.cnblogs.com/yangyh/archive/2011/06/04/2072393.html 首先这位作者, 因为看了这篇简短的一个博文, 我相同了关于虚函数方面的 ...
- Android保持屏幕常亮唤醒状态
第一步: 首先添加权限: <uses-permission android:name="android.permission.WAKE_LOCK"></uses ...
- c语言字符串内存分配小记
一.疑问 有这样一道题: #include "stdio.h" int main() { ]; ]; scanf("%s", word1); scanf(&qu ...
- 【Python】Python PYQT4 GUI编程与exe打包
本篇文章承接http://www.cnblogs.com/zhang-zhi/p/7646923.html#3807385,上篇文章描述了对文本文件的简单处理,本章节结合PYQT4实现该功能的GUI图 ...
- C++树的建立和遍历
#include<iostream.h> typedef char TElemtype; typedef struct Btree { TElemtype data; struct Btr ...
- Python全栈 MySQL 数据库 (SQL查询、备份、恢复、授权)
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想为了信仰 开局一张图 今天接着昨天的说 索引有4种: 普通 索引 :ind ...
- CSU-2110 Keeping Cool
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2110 题目 Description Kevin has just got ...