Shuffle'm up (poj 3087 simulation)

Source: Internet
Author: User
Tags shuffle uppercase letter
Language:DefaultShuffle'm up
Time limit:1000 ms   Memory limit:65536 K
Total submissions:5968   Accepted:2802

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:

It is known that the initial states of the two heap cards S1 and S2 are all C, which are sequentially stacked into a pile of S12 cards, and then the bottom C cards of S12 are classified as S1, the top C card is classified as S2, and the cycle goes down accordingly.

Enter the initial and final statuses of S1 and S2, S12

Ask how many shuffles S1 S2 can reach S12. If it is never the same, it will output "-1 ". Directly simulate the process.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int main(){    int N,C,cas=1;    char s1[110],s2[110],str[222],s[222];    scanf("%d",&N);    while (N--)    {        map<string,int>q;        scanf("%d",&C);        scanf("%s%s%s",s1,s2,str);        bool ok=true;        int ans=0;        while (1)        {            int num=0,len1=0,len2=0;            while (num<2*C)            {                if (num%2)                    s[num++]=s1[len1++];                else                    s[num++]=s2[len2++];            }            s[num]='\0';            ans++;            if (strcmp(str,s)==0)                break;            if (q[s])            {                ok=false;                break;            }            q[s]=1;            for (int i=0;i<C;i++)                s1[i]=s[i];            s1[C]='\0';            for (int i=C;i<2*C;i++)                s2[i-C]=s[i];            s2[C]='\0';        }        if (ok)            printf("%d %d\n",cas++,ans);        else            printf("%d -1\n",cas++);    }    return 0;}/*24AHAHHAHAHHAAAAHH3CDECDEEEDDCC*/


Shuffle'm up (poj 3087 simulation)

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.