[cf][R#467C][构造题]Lock Puzzle
2018-02-27 09:57
232 查看
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
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.
相关文章推荐
- CF 584E(Anton and Ira-构造+贪心)
- CF 482A(Diverse Permutation-相邻距离不同数为k的1~n全排列构造)
- CF - 805C. Find Amir - 贪心思维+构造
- CF 568 C 构造
- cf#Wunder Fund Round 2016 -B- Guess the Permutation-构造-乱搞
- CF 803C Maximal GCD 枚举,构造
- 【打CF,学算法——二星级】CodeForces 520C DNA Alignment (构造)
- 【构造策略】-CF-301A-Yaroslav and sequence
- CF 715A Plus and a square root(数学构造)
- CF 862C Mahmoud and Ehab and the xor 构造
- CF 518A(Vitaly and Strings-构造)
- CF 553B 构造
- CF - 484 - A. Bits(构造)
- CF 277.5 C.Given Length and Sum of Digits.. 构造
- cf 389D Fox and Minimal path 【构造】
- CF - 733C 构造 + 贪心
- [CF803A] Maximal Binary Matrix(构造)
- cf - 148 -- c 特殊判定 构造特定序列
- CF 549G排序,构造
- CF 482A(Diverse Permutation-相邻距离不同数为k的1~n全排列构造)