CodeForces 936C Lock Puzzle

Source: S o u r c e : Codeforces Round #467 (Div. 1)
Problem: P r o b l e m : Shift(n) S h i f t ( n ) 操作使得字符串 p=αβ p   =   α β 变成 βRα β R α ,其中 Length(β)=n L e n g t h ( β ) = n ,最多进行 3n 3 ∗ n 步操作,使得字符串 s s 转变为t
Idea: I d e a : 假设当前字符串 s s 形如b...p...,其中 b=suffix(t) b = s u f f i x ( t ) pb=suffix(t) p b = s u f f i x ( t ) p p 的下标为x。先 Shift(n) S h i f t ( n ) s=...p...bR s = . . . p . . . b R ,再 Shift(x1) S h i f t ( x − 1 ) , s=b..p s = b . . p ,最后 Shift(1) S h i f t ( 1 ) s=pb... s = p b . . .
Code: C o d e :

#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define CLR(A, X) memset(A, X, sizeof(A))
#define bitcount(X) __builtin_popcountll(X)
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<char, int> PCI;
const double eps = 1e-10;
const double PI = acos(-1.0);
const auto INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
int dcmp(double x) { if(fabs(x) < eps) return 0; return x<0?-1:1; }

const int MAXN = 2e3+10;

char s[MAXN], t[MAXN];
int n;
vector<int> G;

void shift(int x) {
    if(x == 0) return;
    reverse(s, s+n);
    reverse(s+x, s+n);
    G.pb(x);
}

int main() {
    scanf("%d%s%s", &n, s, t);
    for(int i = 0; i < n; i++) {
        int j = i;
        while(j<n && s[j]!=t[n-i-1]) j++;
        if(j == n) {
            puts("-1");
            exit(0);
        }
        shift(n);
        shift(j);
        shift(1);
    }
    printf("%d\n", G.size());
    for(int x:G) {
        printf("%d ", x);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值