POJ 3087 Shufflem Up(模拟)

Shuffle’m Up

Time Limit:1000MSMemory Limit:65536K

Total Submissions:7404Accepted:3421

Description

A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed 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 performed by interleaving a chip fromS1with a chip fromS2as shown below forC= 5:

The single resultant stack,S12, contains 2 *Cchips. The bottommost chip ofS12is the bottommost chip fromS2. On top of that chip, is the bottommost chip fromS1. The interleaving process continues taking the 2ndchip from the bottom ofS2and placing that onS12, followed by the 2ndchip from the bottom ofS1and so on until the topmost chip fromS1is placed on top ofS12.

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 then be repeated to form a newS12.

For this problem, you will write a program to determine if a particular 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 of theCchips in stackS1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of theCchips 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 shuffling ofS1andS2zero 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

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<map>#include<string>using namespace std;string s1,s2,str1,str2,str;string s;int main(){int T;int k = 0;scanf("%d",&T);while(T–){int n;scanf("%d",&n);cin >> s1;cin >> s2;cin >> s;map<string,bool>p;p.clear();p[str1] = 1;p[str2] = 1;int flag = 1;int t = 0;str1 = s1;str2 = s2;while(flag){t++;str = "";for(int i=0; i<n; i++){str += str2[i];str += str1[i];}if(str == s){printf("%d %d\n",++k,t);flag = 0;}else if(p[str] == 1){flag = 0;printf("%d %d\n",++k,-1);}else{str1 = "";str2 = "";for(int i=0; i<n; i++){str1 += str[i];}for(int i=n; i<2*n; i++){str2 += str[i];}}p[str] = 1;}}return 0;}

版权声明:本文为博主原创文章,如有特殊需要请与博主联系 QQ : 793977586。

,见过旅行风景,就这样,慢慢学会了长大。

POJ 3087 Shufflem Up(模拟)

相关文章:

你感兴趣的文章:

标签云: