Codeforces936C. Lock Puzzle

给个串,只能用操作shift x表示把后面x个字符翻转后放到串的前面。问s串怎么操作能变t串。n<=2000,操作次数<=6100。

打VP时这转来转去的有点晕。。。

可以想一种逐步构造的方法,即从一个小的完成构造的部分通过一顿操作,在不影响这部分的前提下扩展。

好吧我看题解了,直接丢图,是从abc扩展成xabcy的方法,如果反了就把他最后再倒过来。

操作次数是$\frac{5}{2}n$的,复杂度$kn$,$k$指操作次数。

 1 //#include<iostream>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cstdio>
 5 //#include<map>
 6 #include<math.h>
 7 //#include<time.h>
 8 //#include<complex>
 9 #include<algorithm>
10 using namespace std;
11  
12 int n;
13 #define maxn 10011
14 char s[maxn],t[maxn];int cnts[30],cntt[30],ans[maxn],lans=0;
15 
16 char tmp[maxn];
17 void shift(int x)
18 {
19     if (x==0) {lans--; return;}
20     memcpy(tmp,s,sizeof(char)*(n+3));
21     int cnt=0; x=n-x+1;
22     for (int i=n;i>=x;i--) s[++cnt]=tmp[i];
23     for (int i=1;i<x;i++) s[++cnt]=tmp[i];
24 }
25 
26 int findpos(int p,int rr)
27 {
28     for (int i=rr;i;i--)
29         if (s[i]==t[p]) return i;
30     return maxn*2;
31 }
32 
33 int main()
34 {
35     scanf("%d",&n);
36     scanf("%s",s+1); scanf("%s",t+1);
37     for (int i=1;i<=n;i++) cnts[s[i]-'a']++,cntt[t[i]-'a']++;
38     for (int i=0;i<26;i++) if (cnts[i]!=cntt[i]) {puts("-1"); return 0;}
39     
40     int p1=(1+n)>>1,p2=p1;
41     int p=findpos(p1,n); p1--; p2++;
42     if (p!=n) {ans[++lans]=n-p; shift(n-p);}
43     bool rev=0;
44     for (int now=1,p;p1;p1--,p2++,now+=2,rev^=1)
45     {
46         if (rev==0) p=findpos(p1,n-now); else p=findpos(p2,n-now);
47         shift(ans[++lans]=n-p);
48         shift(ans[++lans]=n);
49         shift(ans[++lans]=now);
50         if (rev==0) p=findpos(p2,n); else p=findpos(p1,n);
51         shift(ans[++lans]=n-p+1);
52         shift(ans[++lans]=p-now-2);
53     }
54     if (n&1) {if (rev) shift(ans[++lans]=n);}
55     else
56     {
57         if (rev) shift(ans[++lans]=n-1);
58         else
59         {
60             shift(ans[++lans]=n-1);
61             shift(ans[++lans]=1);
62             shift(ans[++lans]=n);
63         }
64     }
65     
66     printf("%d\n",lans);
67     for (int i=1;i<=lans;i++) printf("%d ",ans[i]);
68     return 0;
69 }
View Code

还有一种好理解的逐个字符构造,也是从后往前。

比如说现在串是AzB,A的前缀已经是t的一个后缀,z是想加在A前面的字符,B是剩下的。然后这样:AzB->B'zA'->AB'z->zAB'。搞定。操作次数3*n。

(好吧这是评论写的)

转载于:https://www.cnblogs.com/Blue233333/p/8477002.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/281169.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

公共服务领域英文译写规范_公共领域日:对版权和公共领域重要性的思考

公共服务领域英文译写规范The first of the year is Public Domain Day, a day intended to call attention to copyright issues and the public domain. At the Center for the Study of the Public Domain they have an interesting (and sobering) review of works that wo…

Elasticsearch 实战经验总结

Centos7下es 7.7.0安装配置 怎么安装使用elasticsearch-head插件 用logstash同步Mysql数据到ES Springboot使用ES官方推荐方式REST Client整合ES实现关键词高亮 ELK-Elasticsearch&#xff0c;Logstash&#xff0c;kibana搭建基于日志文件的日志分析系统 设置elasticsearc…

.Net 7 的 AOT 和 CLR有什么区别?

楔子&#xff1a;AOT和 CLR的区别是什么呢&#xff1f;大部分人肯定会说&#xff0c;一个编译成本地机器码&#xff08;Native Code&#xff09;&#xff0c;一个是JIT即时编译的结果。这么说&#xff0c;其实也对&#xff0c;但是不具体。具体应该怎么看呢&#xff1f;AOTAOT实…

接入amazon avs_每日新闻综述:亚马逊将互联网接入推向全球的宏伟计划

接入amazon avsPlus Snap’s big push to stay relevant, Amazon’s Alexa-powered AirPods alternatives, more Android Q news, and a lot more. It’s time to talk about the biggest, coolest, or generally most interesting stories from the last 24 hours. 加上Snap保…

计算的未来

我自己倒是后来也是觉得我自己可以想象一个未来的技术&#xff0c;就是以后的编程的语言和库可以抽象现在的一些高级语言的关键字。比如要写一个编辑器的时候&#xff0c;只要给点这些东西的数据结构和数据流向&#xff0c;而一些什么很繁琐的一些底层编码都是可以用高级语言来…

nginx 实用配置问题总结

配置 tomcat&#xff0c;nginx&#xff0c;解决 post 请求超时问题nginx 跨域问题 CORS policy: No Access-Control-Allow-Originnginx 配置静态验证文件&#xff0c;报 404&#xff0c;解决方案nginx 获取用户真实 IPcentos 部署 php 网站方法-使用 nginx ssl https

零部件分类属性

离散制造业的研发、生产跟产品零部件紧密联系在一起&#xff0c;从企业业务流程来说零部件涉及研发、采购、仓储、生产、质量、售后和配件等多个部门&#xff0c;为了更好地管理零部件&#xff0c;下面我们一起来看看零部件概念及分类。1、按行业属性分类&#xff08;1&#xf…

键盘忍者:使用单个热键弹出Vista日历

We’ve covered how to access the Windows Vista Calendar using the keyboard, but what if you wanted to assign a single keystroke to pop up the calendar? Yeah, sure, you can just click it with the mouse, but where’s the geek fun in that? 我们已经介绍了如何…

Linux下全局安装composer方法

# 下载composer [vagrantlocalhost ~]$ curl -sS https://getcomposer.org/installer | php# 将composer.phar文件移动到bin目录以便全局使用composer命令 [vagrantlocalhost ~]$ mv composer.phar /usr/local/bin/composer# 切换国内源 [vagrantlocalhost ~]$ composer config…

如何使用必应地图 WPF 控件

如何使用必应地图 WPF 控件如何使用必应地图 WPF 控件作者&#xff1a;WPFDevelopersOrg - 驚鏵原文链接&#xff1a;https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用.NET40&#xff1b;Visual Studio 2019;Bing Maps WPF 控件需要 .NET Framework 4.0和 Windows S…

如何保存推特链接以供以后从台式机和手机阅读

Have you come across a lot of interesting links from Twitter, but you don’t have the time to read all of them? Today we’ll show you how to read these links later from your desktop and phone. 您是否遇到过Twitter上很多有趣的链接&#xff0c;但没有时间阅读所…

scrapy爬虫实战分享

自动登录脚本参考scrapy爬虫启示录-小伙子老夫看你血气方刚这本《爬虫秘录》就传给你了Scrapy初章-Scrapy理论简介Scrapy次章-啥也不干就是爬图Scrapy第四章-设置代理IP偷偷爬图Scrapy第三章-图片存库MysqlScrapy第五章-多线程加速爬图Scrapy终章-1024福利Scrapy最最最终章-搂一…

【重大更新】DevExpress v17.2新版亮点—Bootstrap篇(二)

用户界面套包DevExpress v17.2日前终于正式发布&#xff0c;本站将以连载的形式为大家介绍各版本新增内容。本文将介绍了Bootstrap Controls v17.2 的CardView、Charts、Editors、GridView、Layout等新功能&#xff0c;快来下载试用新版本&#xff01; GridView Toolbar 在此版…

盘点 .NET 7 新功能

点击上方蓝字关注我们&#xff08;本文阅读时间&#xff1a;20分钟)本文翻译于 Jeremy Likness, Angelos Petropoulos 和 Jon Douglas 的博客.NET 7 为C# 11/F# 7、.NET MAUI、ASP.NET Core/Blazor、Web API、WinForms、WPF 等应用程序带来了更高的性能和新功能。使用 .NET 7&a…

onlyoffice采坑笔记

中文版onlyoffice/documentserver镜像制作onlyoffice 20并发限制处理&#xff0c;up to 20 maximumonlyoffice安装-Linuxwindows 10 下用docker安装onlyoffice服务 onlyoffice安装-Linux 0 点赞 ⋅ 0 回复 ⋅ 3月前onlyoffice相关命令记录 0 点赞 ⋅ 0 回复 ⋅ 3月前onlyoffice…

nb-iot链路层加密_Google为低端Android手机和IoT设备创建了更快的加密

nb-iot链路层加密Google谷歌Low-resource Android phones and IoT devices don’t have the processing power to use modern encryption services, which makes them vulnerable to hacking. That’s why Google is introducing Adiantum, a super-fast encryption standard f…

用offset调用文章

一。用offset偏移调用文章&#xff0c;这个我认为是比较好的&#xff0c;经常用。容易控制1. 第一个post显示5篇query_posts(showpost 5); 2.那么第二个post显示除上面显示过的6篇query_posts(showpost 6 & offset5); 二、用置顶文章1.第一个Post显示置顶文章query_posts(s…

MediatRPC - 基于MediatR和Quic通讯实现的RPC框架,比GRPC更简洁更低耦合,开源发布第一版...

大家好&#xff0c;我是失业在家&#xff0c;正在找工作的博主Jerry。作为一个.Net架构师&#xff0c;就要研究编程艺术&#xff0c;例如SOLID原则和各种设计模式。根据这些原则和实践&#xff0c;实现了一个更简洁更低耦合的RPC&#xff08;Remote Procedure Calls&#xff09…

新鲜抓取古文赏析五千篇

新鲜抓取的古文&#xff0c;有感兴趣的可以来看看。-IT源点-古文赏析 外科精義 黄景昌-古诗文选集 鼎镌陈眉公先生批评西廂记 世醫得效方 汪炎昶-古诗文选集 至正条格 乐郊私语 敖氏傷寒金鏡錄 十四經發揮 宋史 草泽狂歌 世医得效方 : 二十卷. 卫生宝鉴 辽史 陈深-古诗文选集 …

wii拆机_设置防砖保护以保护和增强Wii

wii拆机We’ve shown you how to hack your Wii for homebrew software, emulators, and DVD playback, now it’s time to safeguard your Wii against bricking and fix some annoyances—like that stupid “Press A” health screen. 我们已经向您展示了如何破解Wii的自制软…