CodeForces 936C Lock Puzzle

S o u r c e : Codeforces Round #467 (Div. 1)
P r o b l e m : S h i f t ( n ) 操作使得字符串 p = α β 变成 β R α ,其中 L e n g t h ( β ) = n ,最多进行 3 n 步操作,使得字符串 s 转变为 t
I d e a : 假设当前字符串 s 形如 b . . . p . . . ,其中 b = s u f f i x ( t ) p b = s u f f i x ( t ) p 的下标为 x 。先 S h i f t ( n ) s = . . . p . . . b R ,再 S h i f t ( x 1 ) , s = b . . p ,最后 S h i f t ( 1 ) s = p b . . .
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;
}

猜你喜欢

转载自blog.csdn.net/qq_32506797/article/details/79377650