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 ...
随机推荐
- Android开发二维码之坑
之前一直做的是.NET开发用的是C#语言,近段时间由于做一个APP这才用上了java,在二维码扫描整合到APP里面遇到扫描二维码之后没有返回值,经过反复的尝试最后终于拿到了返回值,之后觉得很有必要记录 ...
- Scala 学习记录(一)
1. 相对于java,scala的值修饰用val,变量修饰用var.值相当于java的final 修饰了. package demo object ScalaBase extends App { pr ...
- 利用JSTL重写查询的jsp页面
利用JSTL重写Java Web MVC实例中的jsp页面 第一步:导入jstl.jar和standard.jar文件
- [Github筆記] 清除所有 Commit 紀錄
# 把原來的 git 移除掉 sudo rm .git -r # 初始化 git init git remote add origin https://github.com/username/repo ...
- [Python學習筆記] 抓出msg信件檔裡的附件檔案
想要把msg信件檔案的附件抓出來做處理,找到了這個Python 模組 msg-extractor 使用十分容易,但是這個模組是要在terminal裡執行,無法直接打在IDLE的編輯器上 所以稍微做了修 ...
- (译文)IOS block编程指南 3 概念总览
Conceptual Overview(概览) Block objects provide a way for you to create an ad hoc function body as an ...
- python实战教程之自动扫雷(自己存下来学习之用)
3.python的第三方库win32api,win32gui,win32con,Pillow,numpy,opencv可通过 pip install --upgrade SomePackage 来进行 ...
- mysql 函数tree状
// 子节点的查询 CREATE DEFINER = `root`@`%` FUNCTION `getDeptChildList`(rootId BIGINT) RETURNS longtext DE ...
- Asp.Net Core 入门(一)——Program.cs做了什么
ASP.NET Core 是微软推出的一种全新的跨平台开源 .NET 框架,用于在 Windows.Mac 或 Linux 上生成基于云的新式 Web 应用程序.国内目前关于Asp.Net Core的 ...
- HTML基础(三)图像和超链接
图像 img 元素向网页中嵌入一幅图像. 语法 <img src="" alt="" /> img标签常用属性 src 跳转的url alt 图片不 ...