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. [奇葩问题] ERROR 2013 (HY000): Lost connection to MySQL server during query

    查询一条耗时30s以上语句,实际为2分钟多. mysql> select version(); +------------+ | version() | +------------+ | 5.6 ...

  2. java浮点数存储

    转自: [解惑]剖析float型的内存存储和精度丢失问题 1.小数的二进制表示问题 首先我们要搞清楚下面两个问题: (1)  十进制整数如何转化为二进制数 算法很简单.举个例子,11表示成二进制数: ...

  3. 阿里云oracle启动失败

    1.监听启动失败: 将$ORACLE_HOME/network/admin 中的listener.ora中的主机名改为localhost 2.sqlplus中startup启动失败 ,出现ORA-00 ...

  4. 第10讲:利用SQL语言实现关系代数操作

    一.实现并.交.差运算 1. 基本语法形式:子查询 [union [all] | intersect [all] | except [all] 子查询] ①意义:将关系代数中的∪.∩.- 分别用uni ...

  5. java第三次实验报告

    北京电子科技学院(BESTI) 实验报告 课程: Java程序设计 班级: 1352 姓名: 池彬宁 学号: 20135212 成绩: 指导教师: 娄嘉鹏 实验日期: 2015.6.3 实验密级: 无 ...

  6. 【数据预处理】TIMIT语料库WAV文件转换

    1 问题描述 这两天复现代码.先构造数据集,纯净语音.不同噪声.不同SNR的混合语音.其中纯净语音由两部分组成,IEEE corpus和TIMIT. 一开始我用MATLAB中的audioread读取音 ...

  7. web11 Struts处理表单数据

    电影网站:www.aikan66.com 项目网站:www.aikan66.com游戏网站:www.aikan66.com图片网站:www.aikan66.com书籍网站:www.aikan66.co ...

  8. 软件项目的开发之svn的使用

    Svn简介 SVN全名Subversion,即版本控制系统.SVN与CVS一样,是一个跨平台的软件,支持大多数常见的操作系统.作为一个开源的版本控制系统,Subversion管理着随时间改变的数据.这 ...

  9. 编写了几个Java类,但是一直运行某一个class,这种是因为:main方法写错

    编写了几个Java类,但是一直运行某一个class,这种是因为:main方法写错

  10. 软工1816 · Beta冲刺(7/7)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 协助完成安卓端的整合 完成安卓端的美化 协助制作宣传视频 接下来的计划 &am ...