给个串,只能用操作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. shutil模块详解2

    1.shutil.make_archive() 实际上是调用了两个模块来实现压缩打包的功能. zipfile和tarfile两个模块,shutil的两个封装的模块. zip是压缩文件,文件内存会变小, ...

  2. 《Head First HTML与CSS》的HTML标签、属性

    一个标准的html5页面: <!doctype html> <html lang="zh-cmn-Hans"> <head> <meta ...

  3. EditText输入手机号自动带空格

    xml: <EditText android:id="@+id/edit_main" android:layout_width="match_parent" ...

  4. 浏览器报错问题解决:Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight respons

    FAQ: Failed to load http://www.erpshop.com/index.php: Request header field Content-Type is not allow ...

  5. 一个SAP开发人员的双截棍之路

    由于种种原因,Jerry最近加入了SAP成都研究院的一个演讲俱乐部,这个俱乐部主要是提高大家的英语演讲能力. 说来Jerry也是大一下期和大二上期一次性高分通过四六级考试的,但是当毕业进入SAP成都研 ...

  6. 转--C#编程总结

    C#编程总结--总目录 http://www.cnblogs.com/yank/p/3543423.html

  7. 【vc】高精度时间函数的使用

    方法一: 函数定义如下: int UsSleep(int us);//返回实际的微秒延时时间 代码实现如下: //参数一表示 需要等待的时间 微秒为单位 int UsSleep(int us) { / ...

  8. 让idea调试不进入class文件中去

  9. 微信小程序:this code is a mock one

    问题 微信小程序调用wx.login() 的 success 函数带的code 提示this code is a mock one 解决方法 appid和微信小程序开发工具所登陆用户管理的小程序清单不 ...

  10. 洛谷 P1708 天然气井 题解

    https://www.luogu.org/problemnew/show/P1708 这道题还是比较好的. 读完题目我们先想想如何计算某个天然气井($x_1,y_1$)和中转站($a_1,b_1$) ...