题目传送门

  传送点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. J.U.C

  2. 线段树合并 || BZOJ 5457: 城市

    题面:https://www.lydsy.com/JudgeOnline/problem.php?id=5457 题解: 线段树合并,对于每个节点维护sum(以该节点为根的子树中最大的种类和)和kin ...

  3. ASP.NET Core ResponseCaching:基于 VaryByHeader 定制缓存 Key

    ASP.NET Core ResponseCaching 提供了缓存http响应内容的能力,通过它可以在本地内存中直接缓存http响应内容,这是速度最快的服务端缓存,省却了网络传输与生成响应内容的开销 ...

  4. findStr

    目录 规则 目录结构 src/main.cpp src/findstr.cpp include/findstr.h ft/TestCase.cpp ft/makefile ftbuild.sh mai ...

  5. Ubuntu系统启动后停在(initramfs)

    问题 今天我在启动虚拟机过程 遇到莫名其妙的问题,启动不了.如下图.提示某个文件系统错误了.例如我的就是 /dev/mapper/vagrant--vg-root . 上面问题 可把我急坏了,以为虚拟 ...

  6. 17.3-uC/OS-III消息管理(消息队列使用)

    多值信号量和和互斥信号量主要用来标志事件是否发生和协调资源的访问.如果要给资源赋予内容进行传递, 信号量就力有所不及了.这时候就需要用到 uC/OS 操作系统的另一个内核机制了,那就是消息队列. 2. ...

  7. Oracle启动关闭

    启动: [oracle@oracleSigle ~]$ sqlplus / as sysdba   SQL*Plus: Release 11.2.0.1.0 Production on Wed Nov ...

  8. 【English】【托业】【四六级】写译高频词汇

    大家都知道,四六级翻译每次考的话题不可能原句直接重复,但是,在研究了近几年的四六级真题后,我们惊奇地发现: 写译词汇在重复考! 写译词汇在重复考! 写译词汇在重复考! 因此,小编为大家整理了四六级写译 ...

  9. python框架之Django(15)-contenttype模块

    假如有一个书城系统,需要给作者和书籍加上评论功能.如果给每个表单独建一个评论表,那么我们以后要扩展其它模块评论功能的时候,还需要随之新建一张评论表,会显得很冗余.对于这种情况,Django 给我们提供 ...

  10. python之以字符串形式导入模块

    示例 调用方法 class CorsMiddleware: def process(self): print('from auth.cors.CorsMiddleware.process') cors ...