[cf][R#467C][构造题]Lock Puzzle

C. Lock Puzzle
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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. 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». In order to apply this operation, explorers choose an integer x from 0 to n inclusive. After that, the current string p = αβ changes to βRα, where the length of β is x, and the length of α is n - x. In other words, the suffix of the length x of string p is reversed and moved to the beginning of the string. For example, after the operation «shift 4» the string «abcacb» will be changed with string «bcacab », since α = ab, β = cacb, β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, the length of the strings s and t (1 ≤ n ≤ 2 000).

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

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

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

Examples
inputCopy
6
abacbb
babcba
output
4
6 3 2 3
inputCopy
3
aba
bba
output
-1
In the first example, after applying the operations, the string on the screen will change as follows:

abacbb bbcaba
bbcaba ababbc
ababbc cbabab
cbabab babcba

sol:

看着就是构造题,但是不会做。发现操作数是3n,我一开始的想法是考虑怎么通过3次操作交换两个元素。这样没法做,后来%了一下身边的同学。他告诉我了一个做法,这里口胡一波。
考虑枚举i n->1 ,维护s的结构
s:Ti+1 Ti+2 ... Tn asdas Ti dasd
你先随便选一个字符使他和Ti匹配,接下来我们考虑怎么把Ti放到最左边。如果你要把Ti放到最左边的话,显然Ti要先放到右边,但是这不好做,我们先shiftn
s:asdad Ti asdsd Tn Tn-1 ... Ti+1
然后shift n-pos,pos是Ti的下标
s:Ti+1 Ti+2 ... Tn asdasdasd Ti
然后Ti就到后面去了,你再shift 1,Ti就到左边去了
严格3n

talk is cheap,but no code.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值