Codeforces Round #467 (Div. 2) E -Lock Puzzle

Lock Puzzle

题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t,

shift(x)表示将字符串的最后x个字符翻转后放到最前面。

思路:不会写,看了题解。。。

因为长度为3000,操作为6500,我们考虑每三次操作将一个字符放到最后,并保证其他字符的顺序不变,这样是可以实现的,

如果我们想要将第k个字符移到最后,我们只要shift(n-1-k) , shift(1) , shift(n-1),就能实现啦 。

 1 #include<bits/stdc++.h>
 2 #define fi first
 3 #define se second
 4 #define mk make_pair
 5 #define pii pair<int,int>
 6 #define read(x) scanf("%d",&x)
 7 #define sread(x) scanf("%s",x)
 8 #define dread(x) scanf("%lf",&x)
 9 #define lread(x) scanf("%lld",&x)
10 using namespace std;
11 
12 typedef long long ll;
13 const int inf=0x3f3f3f3f;
14 const int INF=0x3f3f3f3f3f3f3f3f;
15 const int N=1e6+7;
16 const int M=2333;
17 
18 int n;
19 char s[N],t[N];
20 vector<int> ans;
21 void shift(int x)
22 {
23     if(x==0) return;
24     reverse(s, s+n);
25     reverse(s+x, s+n);
26     ans.push_back(x);
27 }
28 int main()
29 {
30     read(n);
31     sread(s); sread(t);
32     for(int i=0;i<n;i++)
33     {
34         int now=0;
35         while(s[now]!=t[i])
36             now++;
37         if(now>=n-i)
38         {
39             puts("-1");
40             return 0;
41         }
42         shift(n-1-now);
43         shift(1);
44         shift(n-1);
45     }
46     printf("%d
",ans.size());
47     for(int i:ans)
48         printf("%d ",i);
49     puts("");
50     return 0;
51 }
52 /*
53 */
原文地址:https://www.cnblogs.com/CJLHY/p/8512055.html