题目传送门

  传送点I

  传送点II

  传送点III

题目大意

  给定一个串$s$询问,有多少对回文子串有交。

  好像很简单的样子。

  考虑能不能直接求,感觉有点麻烦。因为要考虑右端点在当前回文子串内还有区间包含问题。

  那么考虑补集转化。问题转化成,考虑当前的回文串,之前有多少个回文串与它不相交。

  跑一遍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的更多相关文章

  1. Codeforces 17E Palisection 【Manacher】

    Codeforces 17E Palisection E. Palisection In an English class Nick had nothing to do at all, and rem ...

  2. CodeForces 17E Palisection(回文树)

    E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...

  3. [codeforces] 17E Palisection

    原题 题目要求相交的回文串对数,这显然非常难,但是要有一种正难则反的心态,求不出来相交的,求出来不相交的不就好了! 对于每以位置i结尾的字符串,在他后面与他不相交的就是以这个位置为结尾的个数和以这个位 ...

  4. CF17E Palisection(manacher/回文树)

    CF17E Palisection(manacher/回文树) Luogu 题解时间 直接正难则反改成求不相交的对数. manacher求出半径之后就可以差分搞出以某个位置为开头/结尾的回文串个数. ...

  5. 【Codeforces 17E】Palisection

    Codeforces 17 E 题意:给一个串,求其中回文子串交叉的对数. 思路1:用哈希解决.首先求出每个点左右最长的回文串(要分奇数长度和偶数长度),然后记录经过每个点的回文串的个数,以及它们是在 ...

  6. CF17E Palisection manacher

    题面:洛谷(带翻译) 题解: 直接求相交不太好求,所以考虑求不相交的回文串对数. 设ll[i]表示以i为开头的回文串个数,rr[i]表示结尾<=i的回文串个数. 然后不相交的回文串对数显然就是对 ...

  7. CF 17E Palisection 求相交回文串个数

    In an English class Nick had nothing to do at all, and remembered about wonderful strings called pal ...

  8. Codeforces

    Codeforces 7E #include <iostream> #include <cstring> #include <cstdio> #include &l ...

  9. 回文树练习 Part1

    URAL - 1960   Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...

随机推荐

  1. asp.net mvc 路由检测工具

    初学mvc,路由搞不清楚,可以通过一款插件 查看匹配的路由. 工具名<RouteDebugger> 可以在nuget中查询RouteDebugger后,安装.或者在控制台进行安装: pm& ...

  2. antd-mobile Picker组件默认值

    import { createForm } from "rc-form"; @createForm() class TopAdSlots extends Component { @ ...

  3. [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]

    这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...

  4. meat标签

    1.文档兼容模式的定义 Edge 模式告诉 IE 以最高级模式渲染文档,也就是任何 IE 版本都以当前版本所支持的最高级标准模式渲染,避免版本升级造成的影响.简单的说,就是什么版本 IE 就用什么版本 ...

  5. JavaScript 运行机制详解

    一.为什么JavaScript是单线程? JavaScript语言的一大特点就是单线程,也就是说,同一个时间只能做一件事.那么,为什么JavaScript不能有多个线程呢?这样能提高效率啊. Java ...

  6. docker的安装教程

    一.Docker服务安装 Docker 支持以下的 Ubuntu 版本: Ubuntu Precise 12.04 (LTS) Ubuntu Trusty 14.04 (LTS) Ubuntu Wil ...

  7. Phpstorm 2018及2017.3.2激活码(DataGrip WebStorm 激活码)

    PhpStrom的下载地址:https://www.jetbrains.com/phpstorm/ 2018适应 最新版PhpStorm 2018正式版改进了PHP 7支持,改进代码完成功能. 直接用 ...

  8. ssm项目中KindEditor的图片上传插件,浏览器兼容性问题

    解决办法: 原因:使用@ResponseBody注解返回java对象,在浏览器中是Content-Type:application/json;charset=UTF-8 我们需要返回字符串(Strin ...

  9. python语法_while循环_for循环

    while 循环: while 条件: print('''asdasd') print('''asdasd') print('''asdasd') 当条件为True时,持续循环 当条件为Flase时, ...

  10. JVM—内存溢出、OutOfMemoryError、StackOverflowError

    学习jvm时看到几篇非常好的系列文章,转载了: <深入理解Java虚拟机>学习小记一之自动内存管理机制(一) http://my.oschina.net/linuxfelix/blog/1 ...