题目链接

传送门

题意

给你一个串串,问你有多少对回文串相交。

思路

由于正着做不太好算答案,那么我们考虑用总的回文对数减去不相交的回文对数。

而不相交的回文对数可以通过计算以\(i\)为右端点的回文串的个数\(\times\)以\(i+1,i+2\dots,n\)为左端点的回文串的个数计算得到。

以\(i\)为右端点的回文串的个数可以直接用回文树\(O(n)\)求出来,以\(i\)为左端点的回文串的个数反着求一遍即可。

不过要注意本题由于数据范围比较大,使用邻接矩阵会导致\(MLE\),因此需要使用邻接矩阵来代替。

代码实现如下

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL; #define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0) const double eps = 1e-8;
const int mod = 51123987;
const int maxn = 2e6 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; int n;
LL ans, tmp;
LL sum[maxn];
char s[maxn]; struct PAM {
//len数组表示以i为结尾的最长回文子串长度
//tot为结点数,lst为上一个字符加的位置
int N, totedge;
int str[maxn], head[maxn];
int fail[maxn], len[maxn], cnt[maxn], num[maxn], tot, lst;
void init() {
for(int i = 0; i <= n + 1; ++i) {
head[i] = -1;
cnt[i] = len[i] = fail[i] = num[i] = 0;
}
totedge = N = lst = 0; tot = 1; fail[0] = fail[1] = 1; len[1] = -1;
} struct edge {
int v, w, next;
}ed[maxn]; void add(int u, int v, int w) {
ed[totedge].v = v;
ed[totedge].w = w;
ed[totedge].next = head[u];
head[u] = totedge++;
} inline int fi(int u, int v) {
for(int i = head[u]; ~i; i = ed[i].next) {
if(ed[i].v == v) return ed[i].w;
}
return 0;
} inline int add(int c) {
int p = lst;
str[++N] = c;
while(str[N - len[p] - 1] != str[N]) p = fail[p];
if(!(lst = fi(p, c))) {
int now = ++tot, k = fail[p];
len[now] = len[p] + 2;
while(str[N - len[k] - 1] != str[N]) k = fail[k];
fail[now] = fi(k, c);
add(p, c, now);
num[now] = num[fail[now]] + 1;
lst = now;
}
cnt[lst]++;
return num[lst];
}
inline void solve() {
for(int i = tot; i; i--) {
cnt[fail[i]] += cnt[i];
(tmp += cnt[i]) %= mod;
}
}
}pam; int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
scanf("%d", &n);
scanf("%s", s + 1);
pam.init();
for(int i = n; i >= 1; --i) {
sum[i] = (sum[i+1] + pam.add(s[i] - 'a' + 1)) % mod;
}
pam.init();
for(int i = 1; i <= n; ++i) {
int cnt = pam.add(s[i] - 'a' + 1);
(ans += 1LL * cnt * sum[i+1] % mod) %= mod;
}
pam.solve();
printf("%lld\n", (((1LL * tmp * (tmp-1) / 2 % mod) - ans) % mod + mod) % mod);
return 0;
}

Palisection(Codeforces Beta Round #17E+回文树)的更多相关文章

  1. Codeforces 932G Palindrome Partition - 回文树 - 动态规划

    题目传送门 通往???的传送点 通往神秘地带的传送点 通往未知地带的传送点 题目大意 给定一个串$s$,要求将$s$划分为$t_{1}t_{2}\cdots t_{k}$,其中$2\mid k$,且$ ...

  2. Codeforces 932G Palindrome Partition 回文树+DP

    题意:给定一个串,把串分为偶数段 假设分为$s_1,s_2,s_3....s_k$ 求满足$ s_1=s_k,s_2=s_{ k-1 }... $的方案数模$10^9+7$ $|S|\leq 10^6 ...

  3. Codeforces Beta Round #19D(Points)线段树

    D. Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  4. CodeForces 17E Palisection(回文树)

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

  5. 【CF17E】Palisection(回文树)

    [CF17E]Palisection(回文树) 题面 洛谷 题解 题意: 求有重叠部分的回文子串对的数量 所谓正难则反 求出所有不重叠的即可 求出以一个位置结束的回文串的数量 和以一个位置为开始的回文 ...

  6. CF17E Palisection(manacher/回文树)

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

  7. Codeforces.GYM100548G.The Problem to Slow Down You(回文树)

    题目链接 \(Description\) 给定两个串\(S,T\),求两个串有多少对相同回文子串. \(|S|,|T|\leq 2\times 10^5\). \(Solution\) 好菜啊QAQ ...

  8. CF17E Palisection(回文树)

    题意翻译 给定一个长度为n的小写字母串.问你有多少对相交的回文子 串(包含也算相交) . 输入格式 第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式 相交的回文子串 ...

  9. Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)

    题意: 给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1].问划分的方案数. n<=1000000 题解: ...

随机推荐

  1. 阿里云 API 签名机制的 Python 实现

    在调用阿里云 API 的时候,最让人头疼的就是 API 的签名(Signature)机制,阿里云在通用文档中也有专项说明,但是仅仅有基于 Java 的实现代码示例.所以这里基于 Python 来分析下 ...

  2. [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像

    [HeadFrist-HTMLCSS学习笔记]第五章认识媒体:给网页添加图像 干货 JPEG.PNG.GIF有何不同 JPEG适合连续色调图像,如照片:不支持透明度:不支持动画:有损格式 PNG适合单 ...

  3. mapreduce 函数入门 二

    m apreduce三大组件:Combiner\Sort\Partitioner 默认组件:排序,分区(不设置,系统有默认值) 一.mapreduce中的Combiner 1.什么是combiner ...

  4. Visual Studio 调试 —— 附加到进程

    第一步:通过管理员方式打开想要附加到进程的项目. 第二步:在 “附加到进程” 对话框中的 “可用进程” 列表中,找到要附加到的程序.我的以 MyProgressTest 为例.选择调试 / 附加到进程 ...

  5. 第一周第二部分 coursera.org

    即使J(,)=,也不能是完美估计,因为其他数据可能存在误差 取任何颜色并沿着“圆”走,就可以得到相同的成本函数值,右图三个点的J(,)相同 越靠近圆心,J(,)越小 梯度下降算法可以将代价函数J()最 ...

  6. case when else end 在update中的使用

    -- 当mark_way字段的值为'划拨用地'时把该字段值修改为'00'-- 当mark_way字段的值为'出让用地'时把该字段值修改为'01'-- 否则把该字段的值修改为'' update ais_ ...

  7. Python如何进行有背景图片的界面跳转

    一.问题在进行tkinter点击按钮弹出新的界面的时候遇到了下面的问题:[_tkinter.TclError: image "pyimage2" doesn't exist] 原因 ...

  8. Codeforces Round #580 (Div. 1)

    Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...

  9. 《JavaEE开发的颠覆者——Spring Boot实战》是一本好书

    这本书的风格非常好.每一节都是先点明这一块知识的要点,随后就手把手的做出一个最简明.但有能体现核心的实例(大多只有几个Class) 这样的书用来熟悉一门框架,实在是再好不过.

  10. 2019 金蝶java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.金蝶等公司offer,岗位是Java后端开发,因为发展原因最终选择去了金蝶,入职一年时间了,也成为了面试官,之 ...