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.pswp.cn/news/281169.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281169.shtml
英文地址,請注明出處:http://en.pswp.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的自制軟…