Codeforces 17E Palisection - Manacher
好像很简单的样子。
考虑能不能直接求,感觉有点麻烦。因为要考虑右端点在当前回文子串内还有区间包含问题。
那么考虑补集转化。问题转化成,考虑当前的回文串,之前有多少个回文串与它不相交。
跑一遍Manacher。然后先差分再二阶前缀和求出以第$i$个位置为右端点的回文子串的个数。然后再求一次前缀和就可以了。
然后再扫一遍就做完了。
Code
/**
* Codeforces
* Problem#17E
* Accepted
* Time: 186ms
* Memory: 37200k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; const int N = 2e6 + , M = , inv2 = (M + ) >> ; int add(int a, int b) {
return ((a += b) >= M) ? (a - M) : (a);
} int sub(int a, int b){
return ((a -= b) < ) ? (a + M) : (a);
} int n, m;
char s[N], str[N << ];
int mxr[N << ];
int f[N]; inline void init() {
scanf("%d", &n);
scanf("%s", s + );
} void Manacher() {
int R = , cen = ;
str[] = '+';
for (int i = ; i <= n; i++) {
str[++m] = s[i];
if (i != n)
str[++m] = '#';
}
str[++m] = '-'; for (int i = ; i < m; i++) {
if (i < R)
mxr[i] = min(R - i, mxr[(cen << ) - i]);
while (str[i - mxr[i]] == str[i + mxr[i]])
mxr[i]++;
if (i + mxr[i] > R)
R = i + mxr[i], cen = i;
}
} inline void solve() {
Manacher();
for (int i = ; i < m; i++)
f[(i >> ) + ]++, f[((i + mxr[i]) >> ) + ]--;
for (int i = ; i <= n; i++)
f[i] = add(f[i], f[i - ]);
for (int i = ; i <= n; i++)
f[i] = add(f[i], f[i - ]);
int res = sub((f[n] * 1ll * f[n]) % M, f[n]) * 1ll * inv2 % M;
for (int i = ; i <= n; i++)
f[i] = add(f[i], f[i - ]);
f[] = , f[n + ] = ; for (int i = ; i <= n; i++) {
res = sub(res, sub(f[i - ], f[max(i - ((mxr[(i - ) << | ] + ) >> ) - , )]));
if (i < n)
res = sub(res, sub(f[i - ], f[max(i - (mxr[i << ] >> ) - , )]));
}
printf("%d\n", res);
} int main() {
init();
solve();
return ;
}
Codeforces 17E Palisection - Manacher的更多相关文章
- Codeforces 17E Palisection 【Manacher】
Codeforces 17E Palisection E. Palisection In an English class Nick had nothing to do at all, and rem ...
- CodeForces 17E Palisection(回文树)
E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...
- [codeforces] 17E Palisection
原题 题目要求相交的回文串对数,这显然非常难,但是要有一种正难则反的心态,求不出来相交的,求出来不相交的不就好了! 对于每以位置i结尾的字符串,在他后面与他不相交的就是以这个位置为结尾的个数和以这个位 ...
- CF17E Palisection(manacher/回文树)
CF17E Palisection(manacher/回文树) Luogu 题解时间 直接正难则反改成求不相交的对数. manacher求出半径之后就可以差分搞出以某个位置为开头/结尾的回文串个数. ...
- 【Codeforces 17E】Palisection
Codeforces 17 E 题意:给一个串,求其中回文子串交叉的对数. 思路1:用哈希解决.首先求出每个点左右最长的回文串(要分奇数长度和偶数长度),然后记录经过每个点的回文串的个数,以及它们是在 ...
- CF17E Palisection manacher
题面:洛谷(带翻译) 题解: 直接求相交不太好求,所以考虑求不相交的回文串对数. 设ll[i]表示以i为开头的回文串个数,rr[i]表示结尾<=i的回文串个数. 然后不相交的回文串对数显然就是对 ...
- CF 17E Palisection 求相交回文串个数
In an English class Nick had nothing to do at all, and remembered about wonderful strings called pal ...
- Codeforces
Codeforces 7E #include <iostream> #include <cstring> #include <cstdio> #include &l ...
- 回文树练习 Part1
URAL - 1960 Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...
随机推荐
- react 使用hooks
react hooks文档 λ yarn add react@16.7.0-alpha.2 λ yarn add react-dom@16.7.0-alpha.2 设置 state import Re ...
- 主机配置nginx后如何获取访问者IP
he你早的时候写了一个根据访问者ip查询天气的小接口,那时候还没得买域名,没有搞nginx,测试自然没有问题,然后随着一个小工具一块上线了,今天突然发现我的小工具有好几个人在使用了,就想写一个记录访问 ...
- mysql批量更新的两种方式效率试验<二>
Mysql两种批量更新的对比 简介: mysql搭载mybits框架批量更新有两种方式,一种是在xml中循环整个update语句,中间以‘:’隔开,还有一种是使用case when 变相实现批量更新, ...
- jquery 全选/取消全部
html /*主要按钮*/ <td><input type="checkbox" id="checkAllChange" /></ ...
- juqery 点击张三触发李四的方法 trigger(); 和 被选元素前插入指定的内容的方法 brfore();
$('.zc_fabu_img_1').on('click',function(){ $("#upImg img").trigger("click"); }) ...
- IDEA 快捷将创建main函数
在编写代码的时候直接输入psv就会看到一个psvm的提示,此时点击tab键一个main方法就写好了. psvm 也就是public static void main的首字母. 依次还有在方法体内键入f ...
- vue设置初始对象时为空报错
解决办法:在初始化时提供完整的数据结构
- eclipse与hadoop-eclipse-plugin之间的版本对应关系
eclipse与hadoop-eclipse-plugin之间,版本互相不兼容,或者说,版本要求严格. 把hadoop-eclipse-plugin复制到eclipse的plugins目录下以后,如果 ...
- ajax中的同步与异步修改数据的问题
这次项目中因为前端有事儿,项目紧急加个新需求,于是自己硬着头皮上去看了下前端的逻辑后便开始动手了,但是为了简单起见就直接自己写了个ajax调服务来获取数据,然后修改前端定义的全局数据 //ajax来请 ...
- EFM32G232F64时钟树
1.为了熟悉MCU的时钟树,先看看EFM32G232F64的CMU(ClockManagementUnit) 时钟管理单元(CMU)用于管控晶振(时钟源)和各个时钟节点.出于降低功耗和启动时间的目的, ...