POJ-3087-Shuffle'm Up(模拟)

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, S1 and S2, each stack containing C chips. Each stack 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 that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

After the shuffle operation, S12 is 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 may then be repeated to form a new S12.

For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.

Input

The first line of input contains a single integer N, (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 integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are 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 result 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 single line that displays the dataset number (1 though N), 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

2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output

1 2
2 -1

题目大意:

现有字符串s1、s2、s12,其中s1、s2的长度为len,s12的长度为2*len。
是否可以通过一些操作使s1和s2转换合并成s12?
变换的操作规则如下:
假设s1=11111,s2=00000
变换后的序列 s=0101010101
假设s1=12345,s2=67890
变换后的序列 s=6172839405
如果s和s12完全相同,那么输出变换次数
如果不完全相等,s的前半部分作为s1,后半部分作为s2,重复上述过程。
第一行T(1≤T≤1000),代表有T组数据.
每组数据第一行len(1≤len≤100),第二行长度为len的字符串s1,第三行长度为len的字符串s2,第四行长度为2*len的字符串s12。
首先输出处理数据组的编号(编号从1开始)
再输出变换次数并换行。
注意两个数字之间有空格。
对于变换次数,如果无需变换直接得到s12,那么输出0,如果无论怎么变换都不会得到s12,那么输出 -1。

分析:只要我们不断将字符串进行变换即可,如果多次变换后又变回了原来的字符串,那么这个字符串无论怎样变换都无法成功的

#include<stdio.h>
#include<string.h>
const int M1=1e2+5,M2=3e2+5;
char s1[M1],s2[M1],s[M2],s12[M2],s3[M2];
int n,m,i,j,k,len,d,time,c;
int main()
{
	scanf("%d",&n);
	c=1;
	while(n--)
	{
		time=0;//初始化
		scanf("%d",&len);//输入s1,s2字符串的长度 
		scanf("%s%s%s",s1,s2,s12);//输入s1 s2 s12 字符串  
		memset(s3,0,sizeof(s3));
		strcat(s3,s1);//s3字符串是s1和s2字符串的组合 
		strcat(s3,s2);//记录s3字符串用于判断s1和s2多次组合后是否会回到初始值 
		while(1)
		{
			d=0;
			for(i=0;i<len;i++)//将s1和s2字符串每个字符分开合并到s字符串中 
			{
				s[d++]=s2[i];
				s[d++]=s1[i];
			}
			s[d]='\0';
			time++;
			if(strcmp(s,s12)==0)//判断s字符串是否与s12字符串相等 
			{
				printf("%d %d\n",c,time);//成立则输出变换次数结束 
				break;
			}
			if(strcmp(s,s3)==0)//如果多次变换后组成的字符串与初始组成的字符串 
			{ 
				printf("%d -1\n",c);	
				break;         //相重了,那么这以后一定是一个循环,不可能组成 
			} 
			for(i=0;i<len;i++) //s12字符串,输出-1结束 
				s1[i]=s[i];
				s1[len]='\0';  //将s字符串重新拆成s1字符串 
			i=0;
			for(j=len;j<d;j++) //将s字符串重新拆成s2字符串 
				s2[i++]=s[j];
				s2[len]='\0';
		}
		c++;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值