给个串,只能用操作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的更多相关文章

  1. 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 ...

  2. HDU 4708:Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. hdu4708 Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  4. Codeforces Round #467 (Div. 2) E -Lock Puzzle

    Lock Puzzle 题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t, shift(x)表示将字符串的最后x个字符翻转后放到 ...

  5. HDU 4708 Rotation Lock Puzzle (简单题)

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. HDUOJ---(4708)Rotation Lock Puzzle

    Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. Rotation Lock Puzzle

    Problem Description Alice was felling into a cave. She found a strange door with a number square mat ...

  8. HDU 4708 Rotation Lock Puzzle(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 题目大意:给定一个方形矩阵,边长为3-10的奇数.每一圈的数字可以沿着顺时针方向和逆时针方向旋转 ...

  9. hdu 4708 Rotation Lock Puzzle 2013年ICPC热身赛A题 旋转矩阵

    题意:给出一个n*n的矩阵,旋转每一圈数字,求出对角线可能的最大值,以及转到最大时的最小距离. 只要分析每一层就可以了,本来想用地址传递二维数组,发现行不通,改了一下就行了. 这里有个坑,比如: 1 ...

随机推荐

  1. Android设计模式——MVP

    一.什么是MVP MVP:全称 Model-View-Presenter. MVP框架由3部分组成:View层负责显示,Presenter层负责逻辑处理,Model层提供数据. View:负责绘制UI ...

  2. 重新部署mysql遇到的问题

    Windows 2008 Server R2 MySql: 5.7 下载Mysql. 配置mysql环境变量 注册mysql服务.(mysqld install) 移除注册,sc delete mys ...

  3. zabbix显示中文

  4. oracle适配器连接不上解决方案

    Oracle适配器连接不上解决方案 作者:Vashon oracle 的Developer连接不上报错:listener does not currently know of SID given in ...

  5. CentOS下JRE环境变量配置

    很多时候,我们需要在CentOS上部署tomcat,从而搭建web服务器,然JDK/JRE环境是前提,这里就记录一下,在后面的时候直接使用. 下载jre-7u80-linux-x64.tar.gz,并 ...

  6. itop安装中使用nginx安装后不能出现enter itop的问题

    安装中没有出现enter itop,  如下的网络请求给了我们原因 原来使用的是域名请求资源文件,而该域名并不能指向我的服务器,所以安装中资源文件请求不成功,查看了我的nginx配置,如下 [ro ...

  7. Oracle的Central Inventory和Local inventory详解

    很多朋友对Oracle的inventory信息不太了解以至遇到相关的问题不知道如何处理,这篇文章我们将详细讲解Oracle的Central Inventory (oraInventory)和Local ...

  8. 看云&gitbook 写帮助文档 | 专注于文档在线创作、协作和托管

    看云 写帮助文档 | 专注于文档在线创作.协作和托管 https://www.kancloud.cn/manual/thinkphp/1678 https://www.gitbook.com/

  9. 两个div之间的蜜汁间隙

    两个div左右相邻,想让他们紧挨在一起 加了margin:0:padding:0: 不知道为什么还是会有间隙. 然后在两个div的父元素加了:font-size:0: 就终于挨在一起惹.

  10. 尺取法 || emmmm

    给定两个上升的数组,一个数组任取一个数,求两个数差的min 尺取法emm 也不知道对不对 #include <stdio.h> #include <stdlib.h> #def ...