Palindrome

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 433    Accepted Submission(s): 168

Problem Description
Alice like strings, especially long strings. For each string, she has a special evaluation system to judge how elegant the string is. She defines that a string S[1..3n−2](n≥2) is one-and-half palindromic if and only if it satisfies S[i]=S[2n−i]=S[2n+i−2](1≤i≤n).For example, abcbabc is one-and-half palindromic string, and abccbaabc is not. Now, Alice has generated some long strings. She ask for your help to find how many substrings which is one-and-half palindromic.
 
Input
The first line is the number of test cases. For each test case, there is only one line containing a string(the length of strings is less than or equal to 500000), this string only consists of lowercase letters.
 
Output
For each test case, output a integer donating the number of one-and-half palindromic substrings.
 
Sample Input
1
ababcbabccbaabc
 
Sample Output
2

Hint

In the example input, there are two substrings which are one-and-half palindromic strings, $abab$ and $abcbabc$.

 
Source
题意:
给出一个字符串,求有多少这样的子字符串S[1..3n−2](n≥2) 满足:S[i]=S[2n−i]=S[2n+i−2](1≤i≤n),例如 abcbabc 显然n是奇数。
代码:
//要求的就是回文半径相互覆盖的点对有多少,manacher预处理出来奇数长度回文串的中间点的回文半径,用优先队列记录一下到达j点最远能够覆
//盖到的位置,当到达i位置时更新队列(去掉没有用了的点,即到达不了i位置的点),树状数组求i位置前半径中有多少相互覆盖的点并把i加入队列。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MAXN=;
char s[MAXN];
int t,n,p[MAXN<<],a[MAXN<<];
struct cmp{
bool operator () (int &a,int &b)const{
return a+p[a]>b+p[b];
}
};
void manacher()
{
n=strlen(s+);
for(int i=,mx=,id=;i<=n;i++){
p[i]=(mx>i?min(p[id*-i],mx-i):);
while(s[i+p[i]]==s[i-p[i]]) p[i]++;
if(i+p[i]>mx) { mx=i+p[i];id=i; }
}
for(int i=;i<=n;i++) p[i]--;
}
void add(int id,int c)
{
while(id<=MAXN-){
a[id]+=c;
id+=((-id)&id);
}
}
int query(int id)
{
int s=;
while(id){
s+=a[id];
id-=((-id)&id);
}
return s;
}
int main()
{
scanf("%d",&t);
while(t--){
scanf("%s",s+);
manacher();
memset(a,,sizeof(a));
ll ans=;
priority_queue<int,vector<int>,cmp>q;
for(int i=;i<=n;i++){
while(!q.empty()){
int now=q.top();
if(now+p[now]<i){
q.pop();
add(now,-);
}else break;
}
ans+=query(i-)-query(i-p[i]-);
q.push(i);add(i,);
}
printf("%lld\n",ans);
}
return ;
}
 

HDU 6230的更多相关文章

  1. HDU 6230 Palindrome ( Manacher && 树状数组)

    题意 : 给定一个字符串S,问你有多少长度为 n 的子串满足  S[i]=S[2n−i]=S[2n+i−2] (1≤i≤n) 参考自 ==> 博客 分析 : 可以看出满足题目要求的特殊回文子串其 ...

  2. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  4. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  5. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  6. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  7. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

随机推荐

  1. JDK动态代理的简单理解

    转载:http://www.cnblogs.com/luotaoyeah/p/3778183.html 动态代理 代理模式是 Java 中的常用设计模式,代理类通过调用被代理类的相关方法,提供预处理. ...

  2. Eclipse各版本分析比较

    Eclipse最初是由IBM公司开发的替代商业软件Visual Age for Java的下一代IDE开发环境,2001年11月贡献给开源社区,现在它由非营利软件供应商联盟Eclipse基金会. Ec ...

  3. Sprint10

    进展:设置事件提醒部分已经完成,接下来是实现完成后在添加主界面显示已添加的事件及时间,并可设置可用与不可用.

  4. 第二阶段Sprint3

    昨天:查看资料,开始视频录制部分的代码实现 今天:事实现保存到指定路径,并能够选择播放 遇到的问题:自动生成文件名,是否需要自己命名?怎么实现?

  5. Java每日编程day2

    Java每日编程day2 第一题 package com.pcx.day2; /* * 九九乘法表并逆序 */ public class JiuJiu { public static void mai ...

  6. 【CS231N】4、神经网络

    一.疑问 二.常用激活函数 1. Sigmoid ​ sigmoid将输入实数值"挤压"到0到1范围内.更具体地说,很大的负数变成0,很大的正数变成1.它对于神经元的激活频率有良好 ...

  7. 【CSAPP笔记】1. 位、字节、整型

    <Computer Systems a Programmer's Perspective>,机械工业出版社.中文译名<深入理解计算机系统>.作者:(美)Randal E.Bry ...

  8. profibus 的DPV0 和DPV1

    DP的功能经过扩展,一共有3个版本:DP-V0,DP-V1和DP-V2.有的用户手册将DP-V1简写为DPV1. 1.基本功能(DP-V0) (1)总线访问方法:各主站之间为令牌传送,主站与从站间为主 ...

  9. 解决亚马逊云服务器上安装nginx后无法访问的问题

    在亚马逊云服务器上装了Ubuntu系统,使用docker环境搭建nginx,启动nginx容器后,在浏览器输入地址后,显示连接超时. 在网上查了一下说有可能是服务器安全组的设置问题 然后在云服务器的安 ...

  10. 微信小程序组件 日历

    js文件 'use strict'; let choose_year = null,   choose_month = null; const conf = {   data: {     hasEm ...