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.每个本质不同 ...
随机推荐
- Java的poi技术遍历Excel时进行空Cell,空row,判断
/** * 导入信息 */ @Override public List<Object> add(HttpServletRequest request) { // TODO Auto-gen ...
- RF中alert的处理
那么在robotframework中如何处理呢? 我在测试过程中遇到了这么一个窗口: 这种应该是属于Confirm 类型,是无法定位到的,在robotframework中需要这样处理: 1.虽然无法定 ...
- 1、Locust压力测试环境搭建
环境准备:阿里云服务器一台.python2.7.pip Locust 介绍 Locust 是一个开源负载测试工具.使用 Python 代码定义用户行为,也可以仿真百万个用户. Locust 简单易用, ...
- python_way day15 HTML-DAY2、 回顾HTML-CSS 标签(css强制生效),JS(数据类型,时间处理,作用域)
python_way day15 HTML-DAY2 html-css回顾 javascript 一.html-css回顾 增加默认值: . 强制生效,就算在上面,被覆盖上了也会生效 解决缩小页面混乱 ...
- PAT_A1067#Sort with Swap(0, i)
Source: PAT A1067 Sort with Swap(0, i) (25 分) Description: Given any permutation of the numbers {0, ...
- 快速列出大纲.提纲.归纳知识点 思维导图工具Xmind
博客搬迁,给您带来的不便敬请谅解! http://www.suanliutudousi.com/2017/10/23/%E5%BF%AB%E9%80%9F%E5%88%97%E5%87%BA%E5%A ...
- leetcode.字符串.696计数二进制子串-java
1. 具体题目 给定一个字符串 s,计算具有相同数量0和1的非空(连续)子字符串的数量,并且这些子字符串中的所有0和所有1都是组合在一起的.重复出现的子串要计算它们出现的次数. 示例 1 : 输入: ...
- Django(九) xadmin全局配置
xadmin的使用,首先需要对model进行注册,才能在后台管理中进行操作. 1.在app里创建py文件:adminx(必须这个名称) 2.导入xadmin和models里的类,格式如下: 其中lis ...
- 内网端口转发[netsh]
一.利用场景 当前获取目标内网边界区域一台机器,可以通外网和内网也就是存在两块网卡,又通过其他手段获取到内网另外一台机器,但是这台机器不能出外网,所以我们可以使用windows自带netsh命令通过边 ...
- php7 安装时需求的依赖包
php70 php70-bcmath php70-cli php70-common php70-devel php70-fpm php70-gd php70-json php70-mbstring p ...