Description
A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is already med by starting with two stacks of poker chips,S1AndS2, Each stack containingCChips. Each stack may contain chips of several different colors.
The actual shuffle operation is already med by interleaving a chip fromS1With a chip fromS2As shown belowC= 5:
The single resultant stack,S12, Contains 2 *CChips. tHe bottommost chipS12Is the bottommost chip fromS2. On top of that chip, is the bottommost chip fromS1. The interleaving process continues taking the 2nd chip from the bottomS2And placing that onS12, Followed by the 2nd chip from the bottomS1And so on until the topmost chip fromS1Is placed on topS12.
After the shuffle operation,S12Is split into 2 new stacks by taking the bottommostCChips fromS12To form a newS1And the topmostCChips fromS12To form a newS2. The shuffle operation may be repeated to form a newS12.
For this problem, you will write a program to determine if a particle resultant StackS12Can be formed by shuffling two stacks some number of times.
Input
The first line of input contains a single integerN, (1 ≤N≤ 1000) which is the number of datasets that follow.
Each dataset consists of four lines of input. The first line of a dataset specifies an integerC, (1 ≤C≤ 100) which is the number of chips in each initial stack (S1AndS2). The second line of each dataset specifies the colors of each ofCChips in stackS1, Starting with the bottommost chip. The third line of each dataset specifies the colors of each ofCChips in stackS2Starting with the bottommost chip. colors are expressed as a single uppercase letter (AThroughH). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 *CUppercase letters (AThroughH), Representing the colors of the desired result of the shufflingS1AndS2Zero 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 (1 thoughN), 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 1 (? 1) For the number of shuffle operations.
Sample Input
24AHAHHAHAHHAAAAHH3CDECDEEEDDCC
Sample output
1 22 -1
Source
Greater New York 2006
Question: Give You A, B, C three cards, B put one, a put one, put a new pile of cards, so you can see whether it can be C, if not, then the card (number known) Below is a, and the card above is B. Continue to repeat,
If it can be put successfully, the number of outputs; otherwise-1
Simulation questions, explained in the code
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; string SS, E, S; int ans, Len; void BFS () {If (ANS & s = SS) // if it is the same as the first time, it is impossible to succeed. The endless loop return; If (S = e) // return is put successfully; + + ans; // The number of counterfeits increases by INT I, j; string a, B; a = B = ""; for (I = 0; I <Len; I ++) A + = s [I]; for (; I <Len * 2; I ++) B + = s [I]; S = ""; for (I = 0; I <Len; I ++) {S + = B [I]; // first B s + = A [I]; // then a} BFS ();} int main () {int T, CA = 0; int I, j; string a, B; scanf ("% d", & T); While (t --) {CIN> Len> B> A> E; // sorry, I entered B, A ans = 0; S = ""; for (I = 0; I <Len; I ++) // This is the first time to play the card. Therefore, the following output ans + 1 {S + = A [I]; S + = B [I];} Ss = s; BFS (); If (S = e) printf ("% d \ n", ++ ca, ans + 1); else printf ("% d-1 \ n", ++ CA);} return 0 ;}