CF932G-Palindrome Partition【PAM】
正题
题目链接:https://www.luogu.com.cn/problem/CF932G
题目大意
给出一个长度为\(n\)的字符串,将其分为\(k\)段(\(k\)为任意偶数),记为\(p\)。要求满足对于任意\(i\)都有\(p_i=p_{k-i+1}\)。求方案数。
\(1\leq n\leq 10^6\)
解题思路
考虑将字符串化为\(S_1S_nS_2S_{n-1}S_3S_{n-2}...\)这样的形式,可以发现对于原本相同的段在这里就被表示为了一个偶回文子串。
那么问题就变为了划分若干个偶回文子串。设\(f_i\)表示前\(i\)个的方案的话有一种比较简单的做法,建立\(PAM\)后求出每个前缀的所有偶回文后缀,然后暴力转移。
但是这样的是\(O(n^2)\)的,时间复杂度不符合要求,考虑优化。对于一个回文串来说它的所有回文后缀就是它的\(border\)。而\(broder\)有一个性质就是所有\(broder\)的长度可以被划分成\(log\)个等差数列。
我们可以在\(PAM\)上维护这些等差数列,记录\(top_i\)表示节点\(i\)所在的等差数列的顶部,然后每次使用\(top\)往上跳。加入新的\(x\)节点(或者覆盖以前的已经有的节点)的时候累计一下自己作为末尾时所在等差数列方案和就好了
时间复杂度\(O(n\log n)\)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+10,P=1e9+7;
int n,cnt,pos[N],len[N],dis[N],fa[N],top[N],ch[N][26];
char t[N],s[N];int f[N],g[N];
int jump(int x,int p){
while(s[p-len[x]-1]!=s[p])x=fa[x];
return x;
}
int Insert(int x,int p){
x=jump(x,p);
int c=s[p]-'a';
if(!ch[x][c]){
++cnt;len[cnt]=len[x]+2;
int y=jump(fa[x],p);
fa[cnt]=ch[y][c];y=cnt;
dis[y]=len[y]-len[fa[y]];
if(dis[y]!=dis[fa[y]])top[y]=y;
else top[y]=top[fa[y]];ch[x][c]=y;
}
return ch[x][c];
}
int main()
{
scanf("%s",t+1);n=strlen(t+1);
if(n&1)return puts("0")&0;
for(int i=1;i<=n;i+=2)s[i]=t[i/2+1];
for(int i=2;i<=n;i+=2)s[i]=t[n-i/2+1];
len[1]=-1;fa[0]=top[1]=cnt=1;
for(int i=1;i<=n;i++)
pos[i]=Insert(pos[i-1],i);
f[0]=1;
for(int i=1;i<=n;i++){
for(int x=pos[i];x;x=fa[top[x]]){
g[x]=f[i-len[top[x]]];
if(x!=top[x])(g[x]+=g[fa[x]])%=P;
if(!(i&1))(f[i]+=g[x])%=P;
}
}
printf("%d\n",f[n]);
return 0;
}
CF932G-Palindrome Partition【PAM】的更多相关文章
- CF932G Palindrome Partition(回文自动机)
CF932G Palindrome Partition(回文自动机) Luogu 题解时间 首先将字符串 $ s[1...n] $ 变成 $ s[1]s[n]s[2]s[n-1]... $ 就变成了求 ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- Device "/dev/sdg" is not a partition【再续】
之前创建asm磁盘的时候总结过一下错误:http://blog.csdn.net/rhys_oracle/article/details/17029333 当今天情况是这种.例如以下: 在使用open ...
- Uva - 12050 Palindrome Numbers【数论】
题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然 ...
- CF932G Palindrome Partition
思路 首先把字符串变为\(S[1]S[n]s[2]s[n-1] \dots\) 这样原来的一个合法的划分方案就变成了用k个长度为偶数的回文子串划分的方案, 然后直接DP,对i位置,可转移的位置就是它的 ...
- HDU 2018 Multi-University Training Contest 1 Triangle Partition 【YY】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6300 Triangle Partition Time Limit: 2000/1000 MS (Java ...
- 680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 491. Palindrome Number【easy】
Check a positive number is a palindrome or not. A palindrome number is that if you reverse the whole ...
- 洛谷P5496 回文自动机【PAM】模板
回文自动机模板 1.一个串的本质不同的回文串数量是\(O(n)\)级别的 2.回文自动机的状态数不超过串长,且状态数等于本质不同的回文串数量,除了奇偶两个根节点 3.如何统计所有回文串的数量,类似后缀 ...
随机推荐
- linux 系统文件记录
proc系列 /proc/diskstats # 记录磁盘相关信息 http://ykrocku.github.io/blog/2014/04/11/diskstats/
- Git分支创建命令
一. 创建test分支提交步骤 1.列出所有分支 # git branch -a 2.创建test分支 # git branch test 3.切换到test分支: ...
- spring生命周期的应用
1.ApplicationContextAware 实现手工加载bean: 例:https://www.cnblogs.com/wala-wo/p/5119192.html https://www.c ...
- 【转】时冲的CSDN:Linux系统各个目录的作用
请各位移步原文链接:时冲的CSDN 以下仅用于个人梳理,排版方便阅读记忆(原文更优): from my typora: 文章目录 Linux文件系统 LINUX有四种基本文件系统类型: 1.普通文件: ...
- Ubuntu18.04 + NVidia显卡 + Anaconda3 + Tensorflow-GPU 安装、配置、测试 (无需手动安装CUDA)
其中其决定作用的是这篇文章 https://www.pugetsystems.com/labs/hpc/Install-TensorFlow-with-GPU-Support-the-Easy-Wa ...
- Learning ROS: Managing System dependencies
Download and install the system dependencies for turtlesim: roscd turtlesim cat package.xml rosdep i ...
- Linux系统的内核编译
<1>给虚拟机分配2048M内存 <2>配置高可用yum源 <3>下载软件 <1>安装内核源码包 根据依赖性提示,安装对应的包 下载并安装软件包(3个) ...
- FileWriter文件文件字符输出流写入存储数据
1.FileWriter文件字符输出流-写入-存储数据 其中,流关闭之后再调用会报IOException; 其中,与文件字符输入流-写出-读取数据 和 字节输出流-写入-存储数据 不同的是,要先flu ...
- 菜狗、《灵笼》、《时光代理人》,重新审视Z世代的电商逻辑
来源:懂懂笔记 B站还有多少潜力可以挖掘? 虽然B站的最新财报依然还是亏损,但同时也让人看到更多的可能性. 从财报数据的亮点来看,一是营收增长,B站二季度营收为44.95亿元,同比增长72%.营收上B ...
- Dockerfile 自动制作 Docker 镜像(一)—— 基本命令
Dockerfile 自动制作 Docker 镜像(一)-- 基本命令 前言 a. 本文主要为 Docker的视频教程 笔记. b. 环境为 CentOS 7.0 云服务器 c. 上一篇:手动制作Do ...