Poj3087 -- shuffle'm up (small simulation)

Source: Internet
Author: User
Shuffle'm up
Time limit:1000 ms   Memory limit:65536 K
Total submissions:5983   Accepted:2809

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

 

Simple and small simulation, according to the given rules, to see if you can play the desired card. If there is a sequence that once appeared, it will fall into a ring and cannot get the result. Otherwise, the number of times is output.

 

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;char s[1000][500] , s1[300] , s2[300] ;int main(){    int t , tt = 1 , n , l , flag , i , j ;    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        scanf("%s %s", s1, s2);        scanf("%s", s[0]);        flag = 0 ;        for(l = 1 ; l <= 100 ; l++)        {            j = 0 ;            for(i = 0 ; i < n ; i++)            {                s[l][j++] = s2[i] ;                s[l][j++] = s1[i] ;            }            s[l][j] = '\0' ;            if( !strcmp(s[0],s[l]) )            {                flag = 1 ;                break;            }            for(i = 1 ; i < l ; i++)                if( !strcmp(s[i],s[l]) )                    break;            if(i < l) break;            for(i = 0 ; i < n ; i++)                s1[i] = s[l][i] ;            s1[i] = '\0' ;            for( ; i < 2*n ; i++)                s2[i-n] = s[l][i];            s2[i-n] = '\0' ;        }        if( flag )            printf("%d %d\n",tt++, l);        else            printf("%d -1\n", tt++);    }    return 0;}


 

Poj3087 -- shuffle'm up (small 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.