题目链接:

Palindrome

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

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
 
题意:问题目描述的字串有多少个?
 
思路:可以发现这种字符串是关于S[n]和S[2n-1]对称的,所以枚举S[2n-1],看在S[2n-1]的半径范围内有多少个S[n]的半径范围包括S[2n-1],可以manacher找到半径,然后树状数组求答案;
 
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=5e5+10;
char s[maxn];
int r[maxn],sum[maxn],n;
struct cmp
{
bool operator()(int x,int y)
{
return x+r[x]>y+r[y];
}
};
priority_queue<int,vector<int>,cmp>qu;
inline int lowbit(int x){return x&(-x);}
inline void update(int x,int num)
{
while(x<=n)
{
sum[x]+=num;
x+=lowbit(x);
}
return ;
}
inline int query(int x)
{
int ans=0;
while(x)
{
ans+=sum[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int T;scanf("%d ",&T);
while(T--)
{
gets(s);
n=strlen(s);
for(int i=n;i>0;i--)s[i]=s[i-1],sum[i]=0;
s[0]='#';s[n+1]='\n';
int mx=0,id=0;
for(int i=1;i<=n;i++)
{
if(mx>i)r[i]=(r[2*id-i]<(mx-i)?r[2*id-i]:(mx-i));
else r[i]=1;
while(s[i-r[i]]==s[i+r[i]])r[i]++;
if(i+r[i]>mx)mx=i+r[i],id=i;
}
for(int i=1;i<=n;i++)r[i]--;
LL ans=0;int num=0;
while(!qu.empty())qu.pop();
qu.push(1);update(1,1);num++;
for(int i=2;i<=n;i++)
{
while(!qu.empty())
{
int fr=qu.top();
if(fr+r[fr]<i)
{
qu.pop();
num--;
update(fr,-1);
}
else break;
}
ans=ans+num-query(i-r[i]-1);
qu.push(i);update(i,1);num++;
}
printf("%lld\n",ans);
}
return 0;
}

  

hdu6230 Palindrome(manacher+树状数组)的更多相关文章

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

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

  2. 【BZOJ-3790】神奇项链 Manacher + 树状数组(奇葩) + DP

    3790: 神奇项链 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 304  Solved: 150[Submit][Status][Discuss] ...

  3. 【bzoj2565】最长双回文串 Manacher+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6802558.html 题目描述 顺序和逆序读起来完全一样的串叫做回文串.比如acbca是回文串,而abc不是(abc ...

  4. BZOJ 3790 神奇项链(manacher+DP+树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3790 [题目大意] 问最少用几个回文串可以构成给出串,重叠部分可以合并 [题解] 我们 ...

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

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

  6. Palindrome Mouse(2019年牛客多校第六场C题+回文树+树状数组)

    目录 题目链接 题意 思路 代码 题目链接 传送门 题意 问\(s\)串中所有本质不同的回文子串中有多少对回文子串满足\(a\)是\(b\)的子串. 思路 参考代码:传送门 本质不同的回文子串肯定是要 ...

  7. [bzoj2124]等差子序列(hash+树状数组)

    我又来更博啦     2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 941  Solved: 348[Submit][Statu ...

  8. Gym - 101755G Underpalindromity (树状数组)

    Let us call underpalindromity of array b of length k the minimal number of times one need to increme ...

  9. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

随机推荐

  1. HDFS的回收站 && 安全模式

    回收站机制 1). HDFS 的回收站机制由 core-site.xml 中 fs.trash.interval 属性(以分钟为单位)设置,默认是 0,表示未启用.注意:配置数值应该为1440,而配置 ...

  2. ubuntu 18.04 64bit下如何安装python开发工具jupyter

    1.执行一下命令进行安装 sudo apt-get install python3-distutils wget https://bootstrap.pypa.io/get-pip.py sudo p ...

  3. [CF914D]Bash and a Tough Math Puzzle

    给定一个数列$a_1,a_2,...,a_n$,支持两种操作 1 l r x,猜测数列中[l,r]位置上的数的最大公约数$x$,判断这个猜测是否是接近正确的.如果我们可以在数列[l,r]位置中改动至多 ...

  4. 什么是浮动IP

    源地址:https://www.1and1.com/digitalguide/server/know-how/what-is-a-floating-ip/ What is a floating IP? ...

  5. Linux服务器上ftp的搭建和使用

    知识点: 1.  FTP的简介.工作原理 2.在Linux上搭建FTP服务器 参考: 阿里云文档:https://help.aliyun.com/knowledge_detail/60152.html ...

  6. Java 数据结构之双向链表

    一.概述: 1.什么是双向链表: 链表中的每个节点即指向前面一个节点,也指向后面一个节点,就像丢手绢游戏一样,每个人都手拉手 2.从头部插入 要对链表进行判断,如果为空则设置尾节点为新添加的节点,如果 ...

  7. 解题报告:hdu1257 LIS裸题

    2017-09-02 17:28:44 writer:pprp 那个裸题练练手,讲解在之前的博客中提到了 代码如下: /* @theme:hdu1257 @writer:pprp @begin:17: ...

  8. Git 设置 SOCKS 代理

    $ export all_proxy=socks5://127.0.0.1:1080

  9. codeforces781D Axel and Marston in Bitland

    题目链接:codeforces781D 正解:$bitset$+状压$DP$ 解题报告: 考虑用$f[t][0.1][i][j]$表示从$i$出发走了$2^t$步之后走到了$j$,且第一步是走的$0$ ...

  10. 什么是ZooKeeper(一)(通俗易懂)

    以前在做别的项目时用过zk,但没有过多深入的学习,本着通俗易懂.简单方便学习成本低的方式,建议大家耐心看完,如果文章中有不清楚的地方,可发私信进步探讨! 学习zk共分为二部分,第一部分主要以理论为主. ...