hdu6230
hdu6230
题意
给出一个字符串,问有多少个子串 \(S[1..3n-2](n \geq 2)\) 满足 \(S[i]=S[2n-i]=S[2n+i-2] (1\leq i \leq n)\) 。
分析
Manacher 算法预处理下以每个下标为中心的最长回文串的长度。
我们要找的子串由两个奇数长度的回文串重叠部分组成,用一个向量数组存下以每个下标作为左端的奇数长度回文串的中心位置,我们可以维护一个树状数组去计算答案。
code
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e5 + 10;
char s[MAXN << 1], t[MAXN << 1];
int n, m, num[MAXN << 1];
void init() {
n = strlen(s);
t[0] = '$';
for(int i = 1; i <= 2 * n; i += 2) {
t[i] = '#';
t[i + 1] = s[i / 2];
}
t[2 * n + 1] = '#';
t[2 * n + 2] = '!';
m = 2 * n + 1;
int mx = 0, id = 0;
memset(num, 0, sizeof num);
for(int i = 1; i <= m; i++) {
if(mx > i) num[i] = min(mx - i, num[2 * id - i]);
else num[i] = 1;
while(t[i - num[i]] == t[i + num[i]]) num[i]++;
if(i + num[i] > mx) {
mx = i + num[i];
id = i;
}
}
}
int f[MAXN];
void update(int x) {
while(x < MAXN) {
f[x]++;
x += x & -x;
}
}
int query(int x) {
int res = 0;
while(x) {
res += f[x];
x -= x & -x;
}
return res;
}
vector<int> G[MAXN];
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%s", s);
init();
memset(f, 0, sizeof f);
for(int i = 0; i < n; i++) G[i].clear();
for(int i = 2; i <= m; i += 2) {
int x = i / 2 - 1, r = (num[i] - 1) / 2;
if(t[i] != '#') {
num[x] = r;
if(r) G[x - r].push_back(x);
} else num[x] = 0;
}
long long ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < G[i].size(); j++) update(G[i][j]);
ans += query(min(n, i + num[i])) - query(i);
}
printf("%lld\n", ans);
}
return 0;
}
hdu6230的更多相关文章
- hdu6230 Palindrome(manacher+树状数组)
题目链接: Palindrome Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Other ...
- Hdu-6230 2017CCPC-哈尔滨站 A.Palindrome Manacher 主席树
题面 题意:给你一个字符串,问你满足s[i]=s[2n-i]=s[2n+i-2]的子串(这子串长度为3n-2)有多少个,原字符串长度<=5e5 题解:对于这种子串,其实要满足2个回文,跑过一次M ...
随机推荐
- [BZOJ4212]神牛的养成计划
[BZOJ4212]神牛的养成计划 试题描述 Hzwer 成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变,原先决定神 ...
- 周记【距gdoi:110天】
这两个星期都在复习和考试,进度慢了好多.(考试也觉得似乎不是很理想) 姚老要我们写个程序来应对学校的分班问题.然后我们就脑洞打开准备设计一个.写应用程序应该是很烧时间的吧? 接下来搞搞后缀数组,然后还 ...
- 对C++ templates类模板的几点补充(Traits类模板特化)
前一篇文章<浅谈C++ templates 函数模板.类模板以及非类型模板参数>简单的介绍了什么是函数模板(这个最简单),类模板以及非类型模板参数.本文对类模板再做几点补充. 文章目录1. ...
- C&C++——段错误(Segmentation fault)
C/C++中的段错误(Segmentation fault) Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的.来自:http://o ...
- Android webview “location.replace” 不起作用
js解决方法: function locationReplace(url){ if(history.replaceState){ history.replaceState(null, document ...
- [hdu 3949]线性基+高斯消元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 一开始给做出来的线性基wa了很久,最后加了一步高斯消元就过了. 之所以可以这样做,证明如下. 首 ...
- Codeforces Round #506 (Div. 3) 题解
Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...
- Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem
D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...
- es6+最佳入门实践(9)
9.Iterator和for...of 9.1.Iterator是什么? Iterator又叫做迭代器,它是一种接口,为各种不同的数据结构提供统一的访问机制.这里说的接口可以形象的理解为USB接口,有 ...
- Java类的声明和访问介绍
1.类的声明 类本身的声明:对类的声明来说,主要包括类的访问权限声明和非访问修饰符的使用.对于一个普通的Java类(POJO)来说,主要的访问权限修饰符只有两个public和默认权限,内部类可以有pr ...