G-shuffle ' m up POJ 3087 simulation shuffle process, counted as violent search is not too

Source: Internet
Author: User
Tags shuffle uppercase letter


G-shuffle ' m up
Time limit:1000ms Memory limit:65536kb 64bit IO format:%i64d &%i64u
Submit Status Practice POJ 3087

Description

A common pastime for poker players at a poker table was to shuffle stacks of chips. Shuffling chips is performed by starting with the stacks of poker chips, S1 and S2, each stack containing C chips. Each of the stacks may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of this chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd Chip from the bottom of S1 and so on until, the topmost chip from S1 are placed on top of S12.

After the shuffle operation, S12 was split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and The topmost C chips from S12 to form a new S2. The shuffle operation is repeated to form a new S12.

For the problem, you'll write a program to determine if a particular resultant stack S12 can be formed by shuffling Stacks some number of times.

Input

The first line of input contains a single integer N, (1≤n≤1000) which are the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a datasets specifies an integer C, (1≤c≤100) which are the number of chips in each initial stack (S1 a nd S2). The second line of each dataset specifies the colors of each of the C chips in stacks S1, starting with the bottommost chip . The third line of each dataset specifies the colors of each of the C chips in stacks S2 starting with the bottommost chip. Colors is expressed as a single uppercase letter (A through H). There is no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired res Ult of the shuffling of S1 and S2 zero or more times. The bottommost chip ' s color is specified first.

Output

Output for each dataset consists of a displays the dataset number (1 though N), a space, and an integer V Alue which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can is reached using the input for the dataset, display the value negative 1 (? 1) for the number of shuffle operations.

Sample Input

2
4
Ahah
HAHA
Hhaaaahh
3
Cde
Cde
Eeddcc

Sample Output

1 2

2-1


Main topic:

Two stacks of cards are known to S1 and S2 The initial state, the number of cards are C, according to the given rules can cross them together into a pile of cards s12, and then s12 the bottom C block card to S1, the top of the C -block card is S2, according to this cycle down.

Now enter the initial state of S1 and S2 and the expected final state S12

Ask s1 s2 after how many times shuffle, finally can reach the state S12, if never the same, then output "1".


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < Cmath> #include <queue>using namespace std;const int maxn=105;const int inf=999999; #define Lson rt<<1,l,m    #define Rson Rt<<1|1,m+1,rtemplate<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32);    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    For (X=0;c>32;c=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return x;}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');}    -------IO template------char s1[maxn],s2[maxn],s12[maxn<<1],tmp[maxn];int main () {int T;    Read (T);    int n,m,i,j,k,t;    int Cas=1;        while (t--) {write (cas++);        Read (n);        scanf ("%s%s%s", S1,S2,S12); i=5000;//estimates the maximum number of simulations, more than is impossible to reach int ans=0;        strcpy (TMP, "");            while (i--) {ans++;                for (j=0;j<n;j++) {tmp[j*2]=s2[j];            TMP[J*2+1]=S1[J];            } tmp[n*2]= ' + ';            if (strcmp (s12,tmp) ==0) break;            for (j=0;j<n;j++) s1[j]=tmp[j];        for (j=n;j<n<<1;j++) s2[j-n]=tmp[j];        } if (i<=0) printf (" -1\n");    else printf ("%d\n", ans); } return 0;}


G-shuffle ' m up POJ 3087 simulation shuffle process, counted as violent search is not too

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.