[codeforces936C]Lock Puzzle

time limit per test : 2 seconds
memory limit per test : 256 megabytes

Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm!

Of course, there is a code lock is installed on the safe. The lock has a screen that displays a string of n lowercase Latin letters. Initially, the screen displays string s s s. Whitfield and Martin found out that the safe will open when string t will be displayed on the screen.

The string on the screen can be changed using the operation «shift x x x». In order to apply this operation, explorers choose an integer x x x from 0 0 0 to n n n inclusive. After that, the current string p   =   α β p = αβ p=αβ changes to β R α β^Rα βRα, where the length of β β β is x x x, and the length of α α α is n   −   x n - x nx. In other words, the suffix of the length x x x of string p p p is reversed and moved to the beginning of the string. For example, after the operation «shift 4 4 4» the string « a b c a c b abcacb abcacb» will be changed with string « b c a c a b » «bcacab » «bcacab», since α   =   a b α = ab α=ab, β   =   c a c b β = cacb β=cacb, β R   =   b c a c β^R = bcac βR=bcac.

Explorers are afraid that if they apply too many operations «shift», the lock will be locked forever. They ask you to find a way to get the string t on the screen, using no more than 6100 operations.

Input

The first line contains an integer n n n, the length of the strings s s s and t ( 1   ≤   n   ≤   2   000 ) t (1 ≤ n ≤ 2 000) t(1n2000).

After that, there are two strings s s s and t t t, consisting of n n n lowercase Latin letters each.

Output

If it is impossible to get string t t t from string s s s using no more than 6100 6100 6100 operations «shift», print a single number   −   1 - 1 1.

Otherwise, in the first line output the number of operations k ( 0   ≤   k   ≤   6100 ) k (0 ≤ k ≤ 6100) k(0k6100). In the next line output k k k numbers x i x_i xi corresponding to the operations «shift x i x_i xi» (0 ≤ x_i ≤ n)$ in the order in which they should be applied.

Examples

Input

6
abacbb
babcba

Output

4
6 3 2 3

Input

3
aba
bba

Output

-1

题意:
给定两个相同长度的字符串 S S S T T T,有一个操作 s h i f t ( x ) shift(x) shift(x)表示将 S S S分成两个部分 α β αβ αβ其中 β β β的长度为 x x x,然后将 S S S变成 β R α β^Rα βRα ( β R (β^R βR表示将字符串 β β β翻转 ) ) , 问能否让 S S S 6100 6100 6100次操作以内变成 b b b,输出操作方案。

题解:
设现在S串可以表示成 X . . . y . . . X...y... X...y... , X X X T T T的一个后缀, y y y是一个字母, y X yX yX也是 T T T的一个后缀,那么设 a a a y y y的下标,那么经过 s h i f t ( n − a + 1 ) , s h i f t ( n ) , s h i f t ( n − a ) shift(n-a+1),shift(n),shift(n-a) shift(na+1),shift(n),shift(na)三次操作之后,S串将会变成 . . . . X R y ....X^Ry ....XRy
若S串可以表示成 . . . . . y . . . . X R .....y....X^R .....y....XR X X X T T T的一个后缀,y是一个字母, y X yX yX也是 T T T的一个后缀,设 a a a y y y的下标,那么经过 s h i f t ( n − a ) , s h i f t ( 1 ) shift(n-a),shift(1) shift(na),shift(1)两次操作之后,S串将会变成 y X . . . . . yX..... yX.....,那么就这样搞了。

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
char bs[2004],s[2004],t[2004];
int ans[10004],k,n;
void shift(int x){
     ans[++k]=x;
     for(int i=1;i<=x;i++)bs[i]=s[n-i+1];
     for(int i=x+1;i<=n;i++)bs[i]=s[i-x];
     for(int i=1;i<=n;i++)s[i]=bs[i];
}
bool same(int a,int b,int c,int d){
     if(b-a+1!=d-c+1)return 0;
     for(int i=1;i<=b-a+1;i++){
         if(s[a+i-1]!=t[c+i-1])return 0;
     }
     return 1;
}
int LiangJiaJun(){
    scanf("%d",&n);
    scanf("%s",s+1);
    scanf("%s",t+1);
    int bg=0;
    for(int l=n;l>=1;l--){
        if(same(1,l,n-l+1,n)){
            bg=l;
            break;
        }
    }
    for(int pd=0,i=bg+1;i<=n;i++,pd^=1){
        bool flag=0;
        if(!pd){
            for(int j=i;j<=n;j++){
                if(s[j]==t[n-i+1]){
                    shift(n-j+1);
                    shift(n);
                    shift(n-j);
                    flag=1;
                    break;
                }
            }
        }
        else{
            for(int j=1;j<=n-i+1;j++){
                if(s[j]==t[n-i+1]){
                    shift(n-j);
                    shift(1);
                    flag=1;
                    break;
                }
            }
        }
        if(!flag)return puts("-1"),0;
    }
    if(!same(1,n,1,n))ans[++k]=n;
    printf("%d\n",k);
    for(int i=1;i<=k;i++)printf("%d ",ans[i]);
    puts("");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值