POJ 3087 Shuffle & #39; m Up (simulation), poj3087

Source: Internet
Author: User

POJ 3087 Shuffle'm Up (simulation), poj3087

Give two heap cards to S1, s2 is handed to you to wash each heap. There are c cards shuffled each time to get s12. The bottom of s12 is one at the bottom of s12, and then the bottom is one at the bottom of s12. the c on the S1. how many shuffles can be obtained after the S2. the string s you input cannot be output-1.

Simple simulation of s1 + s2! = S: keep shuffling. If you return to the Initial State and do not get s, you will not get s. If you get s, you can output the shuffling times.

#include<iostream>#include<string>using namespace std;int cas, c, ans;string s, bs, s12, s1, s2;void shuf (string s1, string s2){    if (bs == s) return;    ++ans;    int i = -1, j = -1, k;    string ss1, ss2;    for (k = 0; k < c; ++k)        ss1 += (i == j ? s2[++i] : s1[++j]);    for (k = 0; k < c; ++k)        ss2 += (i == j ? s2[++i] : s1[++j]);    s12 = ss1 + ss2;    if (s12 == s || s12 == bs) return;    else shuf (ss1, ss2);}int main(){    cin >> cas;    for (int ca = 1; ca <= cas; ++ca)    {        ans = 0;        cin >> c >> s1 >> s2 >> s;        bs = s1 + s2;        shuf (s1, s2);        if (s12 != s) ans = -1;        cout << ca << " " << ans << endl;    }    return 0;}
Attach this question data

/*114AHAHHAHAHHAAAAHH3CDECDEEEDDCC9ACCABCABCDEFDEFDEFECECECAFAFAFDBDBDB10ABCDEFGHCDAAAAAAAAAABDFHDAAAAAACEGCAAAAA100ABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDABCDEFGHCDCEGCACEGCACEGCACEGCACEGCADFHDBDFHDBDFHDBDFHDBDFHDBEGCACEGCACEGCACEGCACEGCACFHDBDFHDBDFHDBDFHDBDFHDBDGCACEGCACEGCACEGCACEGCACEHDBDFHDBDFHDBDFHDBDFHDBDFCACEGCACEGCACEGCACEGCACEGDBDFHDBDFHDBDFHDBDFHDBDFH5AAAAABBBBBAABBBAAABB6AAAAAABBBBBBAAABBBAAABBB7AAAAAAABBBBBBBABBAABBAABBAAB8AAAAAAAABBBBBBBBBBAABBAABBAABBAA9AAAAAAAAABBBBBBBBBBBBBAAAAABBBBBAAAA10AAAAAAAAAABBBBBBBBBBBBAABBAABBAABBAABBAA1 22 -13 -14 55 306 97 118 29 210 811 2*/

Shuffle'm Up

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




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.