Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)
题意:
给出一个长度为偶数的字符串S,要求把S分成k部分,其中k为任意偶数,设为a[1..k],且满足对于任意的i,有a[i]=a[k-i+1]。问划分的方案数。
n<=1000000
题解:
反正我是不会做 (我是转载的yyb博客,巨佬写的超级超级详细)
基本就是照着laofulaofu的打了一遍(laofu太强啦)
这题分成了两个步骤
如果直接分kk段我们是没法直接判断的
假设两段si,sk−i+1
因为si=sk−i+1=x1x2.....xj
假设si的开始位置为p
假设原串S的长度为n
si=S[p]S[p+1]....S[p+j−1]
sk−i+1=S[n−j−p+1]S[n−j−p+2]...
对应相等的关系是
S[p]=S[n−p−j+1]
如果p=1考虑一下特殊情况
那么就是S[1]=S[n−j]
那么,如果我们有一个串S′
S′=S[1]S[n]S[2]S[n−2].....
那么,对应到上面的相等关系里面,
就是S′[1..j]是一个回文串
其他的每一组对应关系也是如此
所以题目转换成了:
告诉你了S′回答把S′分为若干个长度为偶数的回文串的方案数
(如果没有搞清楚可以手玩一下)
这个就是一个裸的dp了
设f[i]f[i]表示S′[1..i]S′[1..i]划分为若干长度为偶数的回文串的方案数
得到转移方程:
其中,S′[j,i]S′[j,i]是回文串
那么,每一个jj对应的位置相当于是S′[1..i]S′[1..i]的回文后缀
构建出回文树之后沿着last跳fail就可以得到所有的j的位置
这样子的复杂度是O(回文串数量)的,显然会超时,考虑如何优化。
有一个小结论就是一个回文串S的一个后缀T是回文串当且仅当T是S的border。
有一个性质就是一个串的所有border可以划分成O(log)段等差数列。
维护pre[p]表示节点p下一个等差数列的末项。 d[p]表示公差
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e6 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ; struct Palindrome_Automaton {
int len[maxn], next[maxn][], fail[maxn], cnt[maxn];
int num[maxn], S[maxn], sz, n, last, d[maxn], pre[maxn]; int newnode(int l) {
for (int i = ; i < ; ++i)next[sz][i] = ;
cnt[sz] = num[sz] = , len[sz] = l;
return sz++;
} void init() {
sz = n = last = ;
newnode();
newnode(-);
S[] = -;
fail[] = ;
} int get_fail(int x) {
while (S[n - len[x] - ] != S[n])x = fail[x];
return x;
} void add(int c, int pos) {
c -= 'a';
S[++n] = c;
int cur = get_fail(last);
if (!next[cur][c]) {
int now = newnode(len[cur] + );
fail[now] = next[get_fail(fail[cur])][c];
next[cur][c] = now;
num[now] = num[fail[now]] + ;
d[now] = len[now] - len[fail[now]];
pre[now] = (d[now] == d[fail[now]] ? pre[fail[now]] : fail[now]);
}
last = next[cur][c];
cnt[last]++;
} } pam; char str[maxn], s[maxn];
LL dp[maxn], f[maxn]; int main() {
// FIN;
sfs(str + );
int len = strlen(str + ), n = ;
for (int i = ; i <= len; i += ) s[i] = str[++n];
n = len;
for (int i = ; i <= len; i += ) s[i] = str[n--];
dp[] = ;
pam.init();
for (int i = ; i <= len; ++i) {
pam.add(s[i], i);
for (int p = pam.last; p; p = pam.pre[p]) {
f[p] = dp[i - pam.len[pam.pre[p]] - pam.d[p]];
if (pam.d[p] == pam.d[pam.fail[p]]) f[p] = (f[p] + f[pam.fail[p]]) % mod;
if (i % == ) dp[i] = (dp[i] + f[p]) % mod;
}
}
printf("%lld\n", dp[len]);
return ;
}
Palindrome Partition CodeForces - 932G 回文树+DP+(回文后缀的等差性质)的更多相关文章
- 回文树(回文自动机)(PAM)
第一个能看懂的论文:国家集训队2017论文集 这是我第一个自己理解的自动机(AC自动机不懂KMP硬背,SAM看不懂一堆引理定理硬背) 参考文献:2017国家集训队论文集 回文树及其应用 翁文涛 参考博 ...
- 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 ...
- BZOJ4044: [Cerc2014] Virus synthesis(回文树+DP)
Description Viruses are usually bad for your health. How about fighting them with... other viruses? ...
- Codeforces 543D. Road Improvement (树dp + 乘法逆元)
题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...
- Codeforces 1332F - Independent Set(树dp)
题目链接 题意 给出一棵 n 个点的树, 求它的所有非空诱导子图的独立集种类数之和, 对 998244353 取模. n ≤ 3e5. 题解 不妨假设在独立集中的点被染色成 1, 其余不染色; 由于不 ...
- 2019牛客暑期多校训练营(第六场)C Palindrome Mouse (回文树+DFS)
题目传送门 题意 给一个字符串s,然后将s中所有本质不同回文子串放到一个集合S里面,问S中的两个元素\(a,b\)满足\(a\)是\(b\)的子串的个数. 分析 首先要会回文树(回文自动机,一种有限状 ...
- [模板] 回文树/回文自动机 && BZOJ3676:[Apio2014]回文串
回文树/回文自动机 放链接: 回文树或者回文自动机,及相关例题 - F.W.Nietzsche - 博客园 状态数的线性证明 并没有看懂上面的证明,所以自己脑补了一个... 引理: 每一个回文串都是字 ...
- 回文树 Palindromic Tree
回文树 Palindromic Tree 嗯..回文树是个什么东西呢. 回文树(或者说是回文自动机)每个节点代表一个本质不同的回文串. 首先它类似字典树,每个节点有SIGMA个儿子,表示对应的字母. ...
- Palindromic Tree 回文自动机-回文树 例题+讲解
回文树,也叫回文自动机,是2014年被西伯利亚民族发明的,其功能如下: 1.求前缀字符串中的本质不同的回文串种类 2.求每个本质不同回文串的个数 3.以下标i为结尾的回文串个数/种类 4.每个本质不同 ...
随机推荐
- Vue中时间的设置
设置默认属性ct_month: null 方法: //默认显示今天getdatatime(){ this.ct_month= new Date(); }, //默认显示昨天getdatatime(){ ...
- 1.2 JSX 语法
官方文档 https://facebook.github.io/react/docs/jsx-in-depth.html JSX 语法听上去很讨厌,但当真正使用的时候会发现,JSX 的写法在组件的组合 ...
- SPOJ MAXMATCH - Maximum Self-Matching (FFT)
题目链接:MAXMATCH - Maximum Self-Matching Description You're given a string s consisting of letters 'a', ...
- XStream环境设置
为Windows 2000/XP设置路径: 假设安装在c:Program Filesjavajdk目录: 在“我的电脑”右键单击并选择“属性”. 在“高级”选项卡下单击“环境变量”按钮. 现在,改变“ ...
- 用注解实现SpringMvc
在第一次完成spirngmvc代码的基础上: 开始时代码 index.jsp <%@ page contentType="text/html;charset=UTF-8" l ...
- Spring Boot配置随机数
Spring Boot支持在系统加载的时候配置随机数. 添加config/random.properties文件,添加以下内容: #随机32位MD5字符串 user.random.secret=${r ...
- Stack,ArrayDeque,LinkedList的区别
本文首发于cartoon的博客 转载请注明出处:https://cartoonyu.github.io/cartoon-blog 这段时间把疯狂JAVA再看了一遍,发现Stac ...
- 基于Python玩转人工智能最火框架 TensorFlow应用实践✍✍✍
基于Python玩转人工智能最火框架 TensorFlow应用实践 随着 TensorFlow 在研究及产品中的应用日益广泛,很多开发者及研究者都希望能深入学习这一深度学习框架.而在昨天机器之心发起 ...
- 46-Ubuntu-系统信息-1-date和cal查看系统时间
序号 命令 作用 01 date 查看系统时间 02 cal calendar查看日历,-y选项可以查看一年的日历
- Raft——可理解的分布式一致性算法
Raft Understandable Distributed Consensus http://thesecretlivesofdata.com/raft/ 一个直观的动画,便于理解raft算法. ...