POJ 3087 Shuffle'm Up
Description
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, and , each stack containing chips. Each stack may contain chips of several different colors.
The actual shuffle operation is performed by interleaving a chip from with a chip from as shown below for :
The single resultant stack, , contains chips. The bottommost chip of is the bottommost chip from . On top of that chip, is the bottommost chip from . The interleaving process continues taking the chip from the bottom of and placing that on , followed by the 2nd chip from the bottom of and so on until the topmost chip from is placed on top of .
After the shuffle operation, is split into new stacks by taking the bottommost chips from to form a new and the topmost chips from to form a new . The shuffle operation may then be repeated to form a new .
For this problem, you will write a program to determine if a particular resultant stack can be formed by shuffling two stacks some number of times.
Input
The first line of input contains a single integer which is the number of datasets that follow.
Each dataset consists of four lines of input. The first line of a dataset specifies an integer , which is the number of chips in each initial stack ( and ). The second line of each dataset specifies the colors of each of the C chips in stack , starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains uppercase letters (A through H), representing the colors of the desired result of the shuffling of and zero or more times. The bottommost chip’s color is specified first.
Output
Output for each dataset consists of a single line that displays the dataset number ( though ), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative for the number of shuffle operations.
Sample Input
2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
Sample Output
1 2
2 -1
Translation
给定两个长度为的字符串和, 接着给出一个长度为的字符串。
将字符串和通过一定的变换变成,找到最小变换次数。
变换规则如下:
假设,变换后的序列 。类似于洗牌的过程。
Idea
直接暴力模拟洗牌过程,得到目标态就输出变换次数;判断不可能的条件是某一状态前面已经出现过,这样就出现了死循环(用set标记)。
Code
1 |
|