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 ...
随机推荐
- 【学习笔记】深入理解js原型和闭包(3)——prototype原型
既typeof之后的另一位老朋友! prototype也是我们的老朋友,即使不了解的人,也应该都听过它的大名.如果它还是您的新朋友,我估计您也是javascript的新朋友. 在咱们的第一节(深入理解 ...
- iOS 随笔 允许所有不安全网络访问项目
允许任意请求访问app App Transport Security Settings -> Allow Arbitrary Loads YES
- react基础语法(一)元素渲染和基础语法规则
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- WEB前端JS与UI框架
前端Js框架汇总 概述: 有些日子没有正襟危坐写博客了,互联网飞速发展的时代,技术更新迭代的速度也在加快.看着Java.Js.Swift在各领域心花路放,也是煞是羡慕.寻了寻.net的消息,也是振奋人 ...
- finger - 用户信息查找程序
总览 finger [-lmsp ] [user ... ] [user@host ... ] 描述 The finger 显示关于系统用户的信息 参数: -s Finger 显示用户的登录名, 真名 ...
- protobuf的Compiler卸载
一.首先,只用remove命令是不起作用的. 二.找寻到进行make的文件夹目录,然后执行make uninstall命令. 三.通过which protoc 命令,找到protoc所在位置,rm p ...
- c++:delete或free报错,语法正常。
#include <stdio.h> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { ]; memcpy( ...
- MyBatis基本运行环境
MyBatis基本运行环境 1. 创建项目 2.拷贝jar加入到项目中build path jar包 3.创建数据库的表及数据添加 USE [mybatis] CREATE TABLE [dbo].[ ...
- HDU6199 gems gems gems (DP)
题意:有n颗石子 两个人轮流拿 如果上一个人拿了x颗 这个人就可以拿x或x+1颗 问先手能获得与后手的价值差最大是多少 题解:看起来是博弈 其实是DP dp[i][j][0/1]表示当前该0/1拿 拿 ...
- \p{space}|\p{Hex}|\p{Digit}|转译符|\1|\g{-1}|[^ab]|/([^\d\D]+)/
小骆驼 第七章 漫游正则表达式王国 #!/usr/bin/perl use strict; use warnings; $_ = 'ab \ cde f ghijk10.x12ln'; if(/d/) ...