题目链接:

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. 通过map文件了解堆栈分配(STM32、MDK5)--避免堆栈溢出

    环境:STM32F103C8T6,MDK5 在最近的一个项目的开发中,每当调用到一个函数,程序就直接跑飞.debug跟进去看不出什么逻辑错误,但发现函数内局部变量声明之后,全局变量的值被清零,后来查看 ...

  2. LeetCode (236):Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. 4.9版本linux内核的ina220电流检测芯片源码在哪里

    答:在drivers/hwmon/ina2xx.c中,内核配置项为CONFIG_SENSORS_INA2XX Location: -> Device Drivers -> Hardware ...

  4. 转:java 重定向和转发的区别

    response.sendredirect("http://www.foo.com/path/error.html"); 重定向和转发有一个重要的不同:当使用转发时,JSP容器将使 ...

  5. 【pytorch】pytorch基础学习

    目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...

  6. 自学Java测试代码一数据类型、数组使用

    2017-08-22 21:23:37. writer:pprp package test; public class helloWorld { int maxn = 123; //常量,需要定义一个 ...

  7. duff's device

    const duffDevice = (items, process) => { let iterations = Math.floor(items.length / 8); let start ...

  8. poi 取excel单元格内容时,需要判断单元格的类型,才能正确取出

    以下内容非原创,原文链接http://blog.sina.com.cn/s/blog_4b5bc01101015iuq.html ate String getCellValue(HSSFCell ce ...

  9. AtCoder Regular Contest 078D

    两边bfs,先一边找到从1到n的路径并记录下来,然后挨个标记,最后一边bfs找1能到达的点,比较一下就行了 #include<map> #include<set> #inclu ...

  10. 搞懂分布式技术28:微服务(Microservice)那点事

    搞懂分布式技术28:微服务(Microservice)那点事 微服务(Microservice)那点事 肥侠 2016-01-13 09:46:53 浏览58371 评论15 分布式系统与计算 微服务 ...