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 ...
随机推荐
- Excel反序排列
实际工作中有这样一个需求,将Excel列表中所有的条目进行反序排列,有人说这还不简单直接选中某一列按照这列排序(升序或降序)就可以了. 但问题是这里没有可以参考的列,进行排序. 比如: 想转换为: 那 ...
- geotrellis使用(四十一)流水线技术
前言 之前 GeoTrellis 为方便用户将数据(GeoTiff 等遥感影像)导入到 backend (包含 Accumulo.File.Hadoop 等格式)中,编写了一个 ETL 类,该类的输入 ...
- R 540
好久没写题解了嘻嘻嘻,昨天补edu自闭了一天还没补完fg这div3令人愉悦. A: #include <bits/stdc++.h> #define mk(a,b) make_pair(a ...
- 【漏洞复现】Tomcat CVE-2017-12615 远程代码执行漏洞
漏洞描述 [漏洞预警]Tomcat CVE-2017-12615远程代码执行漏洞/CVE-2017-12616信息泄漏 https://www.secfree.com/article-395.html ...
- 10.vue框架
vue框架 易用.灵活.高效 https://cn.vuejs.org/ mvc M model 数据 模型 V view 视图 dom操作 C controller 控制器 路由 C oBtn.on ...
- orace函数
处理字符的函数 lower(char);//把字符串转换成小写格式 upper(char);//把字符转换成大写 length(char);//返回字符串的长度 substr(char,m,n);// ...
- react中改变echart图表的形状
首先说明一点constructor中的只会渲染一次. 父组建是两个点击按钮,点击一个传过来bar,和一个line,子组件也就是当前组建通过this.props.type接收. 渲染是通过::::::t ...
- 微信小程序开发笔记01
微信小程序开发的优势 1,不用安装,即开即用,用完就走.省流量,省安装时间,不占用桌面: 2,体验上虽然没法完全媲美原生APP,但综合考虑还是更优: 3,对于小程序拥有者来说,开发成本更低,他们可以更 ...
- Feign 客户端源码解析
Feign的使用非常简单,增加如下配置之后,便可以使用Feign进行调用.非常简单是不是.主要的工作由Feign框架完成.业务代码只提供了一个Interface, 然后由Feign动态生成代理类来实现 ...
- JavaScript将字典序升序排列类似php中的ksort函数
/** * 将json数据进行排序 * @param {*jason} data */ export function JsonSort(jsonData) { try { let tempJsonO ...