题意:

给你两个串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 (最小回文分解)的更多相关文章

  1. 【CF906E】Reverses(回文自动机,最小回文分割)

    题意:给定两个长度相等的仅由小写字母组成的串A和B,问在A中最少选择多少段互不相交的子串进行翻转能使A和B相同 len<=5e5 思路:构造新串S=a[1]b[1]a[2]b[2]...a[n] ...

  2. 洛谷 P1609 最小回文数 题解

    这题其实并不难,重点在你对回文数的了解,根本就不需要高精度. 打个比方: 对于一个形如 ABCDEFGH 的整数 有且仅有一个比它大的最小回文数 有且仅有一个比它小的最大回文数 而整数 ABCDDCB ...

  3. 洛谷——P1609 最小回文数

    题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. 对于一个给定的N,请你寻找一个回文数P,满足P>N. 满足这样条件的回文数很多, ...

  4. 洛谷—— P1609 最小回文数

    https://www.luogu.org/problemnew/show/1609 题目描述 回文数是从左向右读和从右向左读结果一样的数字串. 例如:121.44 和3是回文数,175和36不是. ...

  5. CodeForces 17E Palisection(回文树)

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

  6. 省选算法学习-回文自动机 && 回文树

    前置知识 首先你得会manacher,并理解manacher为什么是对的(不用理解为什么它是$O(n)$,这个大概记住就好了,不过理解了更方便做$PAM$的题) 什么是回文自动机? 回文自动机(Pal ...

  7. 8633 回文划分(dp)

    8633 回文划分 该题有题解 时间限制:1000MS  内存限制:1000K提交次数:169 通过次数:63 题型: 编程题   语言: G++;GCC Description 我们说一个字符串是回 ...

  8. C语言实现计算双基回文数详解

    双基回文数的定义: 如果一个正整数n至少在两个不同的进位制(二进制<=进制=<十进制)b1和b2下都是回文数,则称n是双基回文数. 根据定义,简单的说就是在二进制到十进制之间(包括十进制和 ...

  9. [Swift]LeetCode866. 回文素数 | Prime Palindrome

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

随机推荐

  1. Java习题练习

    Java习题练习 1. 依赖注入和控制反转是同一概念: 依赖注入和控制反转是对同一件事情的不同描述,从某个方面讲,就是它们描述的角度不同.依赖注入是从应用程序的角度在描述,可以把依赖注入描述完整点:应 ...

  2. python 中的内置高级函数

    1.map(function,iterable) map是把迭代对象依次进行函数运算,并返回. 例子: map返回的十分map对象,需要list()函数转化. 2.exec()函数 执行储存在字符串或 ...

  3. 本地JAR包打入本地mvn仓库

    新建目录my-lib,将jar包移动到目录中,添加pom文件(用alipay测试) <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  4. 拾遗:Qemu/KVM

    WinXP: #!/bin/bash name=winxp qemu-system-x86_64 \ -enable-kvm \ -cpu host -smp ,sockets=,cores=,thr ...

  5. 【转】手机web页面制作时的注意事项

    一.手机页面的标准头规范 字符编码使用utf-8:指定页面手机内存缓存中的存储时间段 device-width:通知浏览器使用设备的宽度作为可视区的宽度 initial-scale - 初始的缩放比例 ...

  6. 第十七章 程序管理与SELinux初探--进程、进程管理(ps、top)

    一个程序被加载到内存当中运行,在内存内的那个数据就被称为进程(process).进程是操作系统上非常重要的概念,所有系统上面跑的数据都会以进程的类型存在.系统进程有哪些状态?不同状态会如何影响系统的运 ...

  7. C# WinForm控件之advTree

    0.属性和方法 //属性方法 advTree1.DragDropEnabled = !advTree1.DragDropEnabled;//控制是否可以拖动节点到 不同的层级下 advTree1.Mu ...

  8. Codeforces Round #535 F-MST Unification

    题目大意: 给定n m 为图中的点数n和边数m 给定m条边的信息 u v w 为u点到v点有一条长度为w的边 图中无环无重边 这个图的MST的花费为k 但可能存在多种花费为k的MST的方案 此时对图中 ...

  9. Pandaria(Kruskal重构树+线段树合并)

    题意 是 有n个花园 一个花园内所有的花的颜色都是一样的 有很多种不同的颜色  花园到花园之间有路,走不同的路有不同的代价   如果选一个点作为起点 只走小于等于w的路  可以经过的这些花园里  那种 ...

  10. 【洛谷】P1229快速幂

    题目链接:https://www.luogu.org/problemnew/show/P1226 题意:求b^p % m之后的结果 题解:快速幂模板 代码: #include<iostream& ...