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 ...
随机推荐
- How to Create a Perl Based Custom Monitor on NetScaler
How to Create a Perl Based Custom Monitor on NetScaler https://support.citrix.com/article/CTX227727 ...
- Word2010 自动生成二级编号
http://jingyan.baidu.com/article/3ea5148901919752e61bbafe.html
- [CF543D]Road Improvement
题目大意:给定一个无根树,给每条边黑白染色,求出每个点为根时,其他点到根的路径上至多有一条黑边的染色方案数,模$1e9+7$. 题解:树形$DP$不难想到,记$f_u$为以$1$为根时,以$u$为根的 ...
- VS查看DLL接口
应用程序Microsoft Visual Studio 2010的Visual Studio Tools文件夹中打开Visual Studio Command Prompt (2010)命令窗口 du ...
- 【2017.12.22.A】
A 题面: 给一个n个点m条边的无向图,你可以选择一个点作为起点,然后沿着图中的边开始走,走的过程中,同一条边不能经过两次(相反的方向也不行). ...
- HDU4289:Control(最小割)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- codeforces 1077D
题目:https://codeforces.com/contest/1077/problem/D 题意:给你一个长度为n的串,你需要在里面找到出现次数最多的长度为k的子序列(子序列中元素可重复),求这 ...
- 使用eclipse插件创建一个web project
使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...
- 金中欢乐赛 A题
题目传送门 这道题就贪心.... 正的一坨和负的一坨间隔 #include<cstdio> #include<cstring> #include<algorithm> ...
- bzoj 1251 裸splay
裸的splay,只需要注意 max[-1]:=-maxlongint 就行了,否则在update的时候子节点的 max值会在max[-1]里选 /*************************** ...