Codeforces936C. Lock Puzzle
给个串,只能用操作shift x表示把后面x个字符翻转后放到串的前面。问s串怎么操作能变t串。n<=2000,操作次数<=6100。
打VP时这转来转去的有点晕。。。
可以想一种逐步构造的方法,即从一个小的完成构造的部分通过一顿操作,在不影响这部分的前提下扩展。
好吧我看题解了,直接丢图,是从abc扩展成xabcy的方法,如果反了就把他最后再倒过来。
操作次数是$\frac{5}{2}n$的,复杂度$kn$,$k$指操作次数。
//#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
//#include<map>
#include<math.h>
//#include<time.h>
//#include<complex>
#include<algorithm>
using namespace std; int n;
#define maxn 10011
char s[maxn],t[maxn];int cnts[],cntt[],ans[maxn],lans=; char tmp[maxn];
void shift(int x)
{
if (x==) {lans--; return;}
memcpy(tmp,s,sizeof(char)*(n+));
int cnt=; x=n-x+;
for (int i=n;i>=x;i--) s[++cnt]=tmp[i];
for (int i=;i<x;i++) s[++cnt]=tmp[i];
} int findpos(int p,int rr)
{
for (int i=rr;i;i--)
if (s[i]==t[p]) return i;
return maxn*;
} int main()
{
scanf("%d",&n);
scanf("%s",s+); scanf("%s",t+);
for (int i=;i<=n;i++) cnts[s[i]-'a']++,cntt[t[i]-'a']++;
for (int i=;i<;i++) if (cnts[i]!=cntt[i]) {puts("-1"); return ;} int p1=(+n)>>,p2=p1;
int p=findpos(p1,n); p1--; p2++;
if (p!=n) {ans[++lans]=n-p; shift(n-p);}
bool rev=;
for (int now=,p;p1;p1--,p2++,now+=,rev^=)
{
if (rev==) p=findpos(p1,n-now); else p=findpos(p2,n-now);
shift(ans[++lans]=n-p);
shift(ans[++lans]=n);
shift(ans[++lans]=now);
if (rev==) p=findpos(p2,n); else p=findpos(p1,n);
shift(ans[++lans]=n-p+);
shift(ans[++lans]=p-now-);
}
if (n&) {if (rev) shift(ans[++lans]=n);}
else
{
if (rev) shift(ans[++lans]=n-);
else
{
shift(ans[++lans]=n-);
shift(ans[++lans]=);
shift(ans[++lans]=n);
}
} printf("%d\n",lans);
for (int i=;i<=lans;i++) printf("%d ",ans[i]);
return ;
}
还有一种好理解的逐个字符构造,也是从后往前。
比如说现在串是AzB,A的前缀已经是t的一个后缀,z是想加在A前面的字符,B是剩下的。然后这样:AzB->B'zA'->AB'z->zAB'。搞定。操作次数3*n。
(好吧这是评论写的)
Codeforces936C. Lock Puzzle的更多相关文章
- hduoj 4708 Rotation Lock Puzzle 2013 ACM/ICPC Asia Regional Online —— Warmup
http://acm.hdu.edu.cn/showproblem.php?pid=4708 Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/O ...
- HDU 4708:Rotation Lock Puzzle
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu4708 Rotation Lock Puzzle
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- Codeforces Round #467 (Div. 2) E -Lock Puzzle
Lock Puzzle 题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t, shift(x)表示将字符串的最后x个字符翻转后放到 ...
- HDU 4708 Rotation Lock Puzzle (简单题)
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDUOJ---(4708)Rotation Lock Puzzle
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Rotation Lock Puzzle
Problem Description Alice was felling into a cave. She found a strange door with a number square mat ...
- HDU 4708 Rotation Lock Puzzle(模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 题目大意:给定一个方形矩阵,边长为3-10的奇数.每一圈的数字可以沿着顺时针方向和逆时针方向旋转 ...
- hdu 4708 Rotation Lock Puzzle 2013年ICPC热身赛A题 旋转矩阵
题意:给出一个n*n的矩阵,旋转每一圈数字,求出对角线可能的最大值,以及转到最大时的最小距离. 只要分析每一层就可以了,本来想用地址传递二维数组,发现行不通,改了一下就行了. 这里有个坑,比如: 1 ...
随机推荐
- Java并发——结合CountDownLatch源码、Semaphore源码及ReentrantLock源码来看AQS原理
前言: 如果说J.U.C包下的核心是什么?那我想答案只有一个就是AQS.那么AQS是什么呢?接下来让我们一起揭开AQS的神秘面纱 AQS是什么? AQS是AbstractQueuedSynchroni ...
- htm 中 <b>和<strong>的区别
显示上两者没有任何区别,都是粗体<b>:为了加粗而加粗,推荐使用 css font-weight 属性来创建粗体文字.<strong>:为了强调而加粗,表示十分重要.在网页中使 ...
- Halcon学习笔记1
转:https://www.cnblogs.com/hanzhaoxin/archive/2013/02/15/2912879.html 机器视觉工程应用主要可划分为硬件和软件两大部分. 硬件:工程应 ...
- Python3 OpenCV3 图像处理基础
开发环境搭建 本人使用的是Ubuntu 16.04LTS. 1.安装Python3 ## 其实 Ubuntu 16.04 系统自带了 Python 3.5.2,因此不需要再安装了?但是需要安装一些开发 ...
- strict说明
- CNN完成mnist数据集手写数字识别
# coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...
- MATLAB读取每个文件夹下的badcsv文件后合并为总的badexcel文件
clear; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%读取子文件夹中bad.csv数据%并把所有数据写到一个excel文件中%%%%%%%%%%%%%%%%%% ...
- windows定时执行python脚本
from:http://blog.csdn.net/Gpwner/article/details/77882131
- h lib dll文件相关部分
参考:https://www.cnblogs.com/azbane/p/7364060.html 只对其中自己用得到的重点做了个笔记. 1..h头文件是编译时必须的,lib是链接时需要的,dll是运行 ...
- catalina配置参数
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.por ...