Reverses CodeForces - 906E (最小回文分解)
题意:
给你两个串s和t,其中t是由s中选择若干个不相交的区间翻转得到的,现在要求求出最少的翻转次数以及给出方案。
1≤|s|=|t|≤500000
题解:
我们将两个字符串合成成T=s1t1s2t2...sntn T=s1t1s2t2...sntn
那么问题就是最少要把整个字符串T 拆分成若干个偶数长度(并且长度大于2)的回文串。
长度是2的表示没有反转。
然后就变成了最小回文分解模型 ,然后直接上板子。
最小回文分解 论文在此
#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 + ;
//最小回文分解
//根据题目需求求出分解成怎样的回文串
//本代码用于将串分解成最小数目的长度偶数回文的方案数
char s[maxn], s1[maxn], s2[maxn]; struct Palindrome_Automaton {
int len[maxn], next[maxn][], fail[maxn], cnt[maxn];
int num[maxn], S[maxn], sz, n, last;
int diff[maxn];//表示相邻回文后缀的等差;
int slk[maxn];//表示上一个等差数列的末项
int fp[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]] + ;
diff[now] = len[now] - len[fail[now]];
slk[now] = (diff[now] == diff[fail[now]] ? slk[fail[now]] : fail[now]);
}
last = next[cur][c];
cnt[last]++;
} //dp[i]表示s[1...i] s[1...i]s[1...i]的最少反转次数。
//pre[i] pre[i]pre[i]表示在最优解里面i为区间右端点的左端点下标。
void solve(int n, int *dp, int *pre) {
for (int i = ; i <= n; i++) dp[i] = INF, pre[i] = ;
init();
dp[] = , fp[] = ;
for (int i = ; i <= n; i++) {
add(s[i], i);
for (int j = last; j; j = slk[j]) {
fp[j] = i - len[slk[j]] - diff[j];
if (diff[j] == diff[fail[j]] && dp[fp[j]] > dp[fp[fail[j]]]) fp[j] = fp[fail[j]];
if (i % == && dp[i] > dp[fp[j]] + ) {//分解成 长度为偶数的回文串
dp[i] = dp[fp[j]] + ;
pre[i] = fp[j];
}
}
if (i % == && s[i] == s[i - ] && dp[i] >= dp[i - ]) {//长度是2的表示没有反转
dp[i] = dp[i - ];
pre[i] = i - ;
}
}
}
} pam; int dp[maxn], pre[maxn]; int main() {
// FIN;
sffs(s1 + , s2 + );
int n = * strlen(s1 + ), len1 = , len2 = ;
for (int i = ; i <= * n; i++) {
if (i & ) s[i] = s1[++len1];
else s[i] = s2[++len2];
}
// fuck(s + 1);
pam.solve(n, dp, pre);
if (dp[n] > n) return * printf("-1\n");
else printf("%d\n", dp[n]);
for (int i = n; i; i = pre[i])
if (i - pre[i] > ) printf("%d %d\n", pre[i] / + , i / );
return ;
}
Reverses CodeForces - 906E (最小回文分解)的更多相关文章
- 【CF906E】Reverses(回文自动机,最小回文分割)
题意:给定两个长度相等的仅由小写字母组成的串A和B,问在A中最少选择多少段互不相交的子串进行翻转能使A和B相同 len<=5e5 思路:构造新串S=a[1]b[1]a[2]b[2]...a[n] ...
- 洛谷 P1609 最小回文数 题解
这题其实并不难,重点在你对回文数的了解,根本就不需要高精度. 打个比方: 对于一个形如 ABCDEFGH 的整数 有且仅有一个比它大的最小回文数 有且仅有一个比它小的最大回文数 而整数 ABCDDCB ...
- 洛谷——P1609 最小回文数
题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. 对于一个给定的N,请你寻找一个回文数P,满足P>N. 满足这样条件的回文数很多, ...
- 洛谷—— P1609 最小回文数
https://www.luogu.org/problemnew/show/1609 题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. ...
- CodeForces 17E Palisection(回文树)
E. Palisection time limit per test 2 seconds memory limit per test 128 megabytes input standard inpu ...
- 省选算法学习-回文自动机 && 回文树
前置知识 首先你得会manacher,并理解manacher为什么是对的(不用理解为什么它是$O(n)$,这个大概记住就好了,不过理解了更方便做$PAM$的题) 什么是回文自动机? 回文自动机(Pal ...
- 8633 回文划分(dp)
8633 回文划分 该题有题解 时间限制:1000MS 内存限制:1000K提交次数:169 通过次数:63 题型: 编程题 语言: G++;GCC Description 我们说一个字符串是回 ...
- C语言实现计算双基回文数详解
双基回文数的定义: 如果一个正整数n至少在两个不同的进位制(二进制<=进制=<十进制)b1和b2下都是回文数,则称n是双基回文数. 根据定义,简单的说就是在二进制到十进制之间(包括十进制和 ...
- [Swift]LeetCode866. 回文素数 | Prime Palindrome
Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...
随机推荐
- DOS和DDOS攻击
1.什么是DOS攻击 首先注意这里是DOS(Denial Of Service)攻击,并非DoS.DoS是微软早期的系统版本. DoS即Denial Of Service,拒绝服务的缩写.DoS是指故 ...
- jenkins在pipline中运行后台命令
需求 在jenkin中启动java程序或者python程序的时候,希望程序在后台执行,并在jenkins构建完成之后继续执行.在工作中有两个地方我碰到了这种需求 在shell script输入框中pi ...
- Devstack 配置文件说明手册
本文为minxihou的翻译文章,转载请注明出处Bob Hou: http://blog.csdn.net/minxihou JmilkFan:minxihou的技术博文方向是 算法&Open ...
- [转] JPA 2.0 with EclipseLink - 教程
原文: http://www.vogella.com/articles/JavaPersistenceAPI/article.html Lars Vogel Version 2.2 Copyright ...
- HTML中<frameset>标签不显示的问题
啥都不说,先上代码 <html> <head> <title>index</title> <meta content = 'text/html'; ...
- Educational Codeforces Round 56 D - Beautiful Graph
题目大意: 在给定的一个图中(可能不连通) 给每个点赋值1.2.3 使得一条边上的两个端点点权相加为奇数 求方案数 一条满足条件的路径上的点权必为一奇一偶交替 偶数只有2 奇数有1.3 若位于1.3 ...
- str2int HDU - 4436 后缀自动机求子串信息
题意: 给出 n 个串,求出这 n 个串所有子串代表的数字的和. 题解; 首先可以把这些串构建后缀自动机(sam.last=1就好了), 因为后缀自动机上从 root走到的任意节点都是一个子串,所有可 ...
- 8u ftp 可以连接但是无法获取目录的解决办法:无法打开传输通道。原因:由于...
来自: http://www.3566t.com/news/dlsn/1557906.html 状态: 正在取得目录列表... 命令: CWD xinghun 响应: 250 OK. Curre ...
- Android的WebView通过JS调用java代码
做项目时候会遇到我们用WebView 打开一个web,希望这个web可以调用自己的一些方法,比如我们在进一个web页面,然后当我们点击web上的某个按钮时,希望能判断当前手机端是否已经登录,如果未登录 ...
- C语言结构体数组
#include <stdio.h> int main() { /*************************************************** *结构体数组:数组 ...