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 ...
随机推荐
- [洛谷P3865]【模板】ST表
题目大意:区间静态最大值 题解:ST表,zkw线段树 ST表: st[i][j]存[i,i+$j^{2}$-1]的最大值,查询时把区间分成两个长度相同的小区间(可重复) #include<cst ...
- bzoj 3456 城市规划 多项式求逆+分治FFT
城市规划 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1091 Solved: 629[Submit][Status][Discuss] Desc ...
- Out of memory due to hash maps used in map-side aggregation解决办法
在运行一个group by的sql时,抛出以下错误信息: Task with the most failures(4): -----Task ID: task_201411191723_723592 ...
- Spring Boot(一)
1.注解 @EnableAutoConfiguration 官方文档:The @EnableAutoConfiguration annotation is often placed on your ...
- Spring学习-- IOC 容器中 bean 的生命周期
Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...
- 用eval转化对象
var str = '{"name": "tom","age": 12,"sex": "man"}' ...
- Topcoder SRM 607 div1题解
好久没来写了,继续继续... Easy(250pts): //前方请注意,样例中带有zyz,高能预警... 题目大意:给你一个字符串,中间有一些是未知字符,请你求出这个字符串的回文子串个数的期望值.数 ...
- 【BZOJ2663】灵魂宝石 [二分]
灵魂宝石 Time Limit: 5 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description “作为你们本体的灵魂,为了能够更好的 ...
- [BZOJ2453]维护队列|分块
Description 你小时候玩过弹珠吗? 小朋友A有一些弹珠,A喜欢把它们排成队列,从左到右编号为1到N.为了整个队列鲜艳美观,小朋友想知道某一段连续弹珠中,不同颜色的弹珠有多少.当然,A有时候会 ...
- python面向对象进阶(上)
一 .isinstance(obj,cls)和issubclass(sub,super) (1)isinstance(obj,cls)检查对象obj是否是类 cls 的对象,返回True和Flase ...