poj3087(模拟)Shuffle'm Up(简单学习一下map)
2016-08-12 14:05
447 查看
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:
![](http://poj.meik.pw/images/3087_1.png)
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
S2 and 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 f(指定)irst.
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9277 | Accepted: 4256 |
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:
![](http://poj.meik.pw/images/3087_1.png)
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
S2 and 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 f(指定)irst.
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
#include<stdio.h> #include<map> #include<string> #include<string.h> using namespace std; int main() { int t; int n; char s1[1000],s2[1000],s12[1000],ss[1000]; scanf("%d",&t); int cou=0; while(t--) { cou++; int flag=0; scanf("%d",&n); map<string,bool>vis; scanf("%s%s%s",s1,s2,ss); vis[ss]=1; int step=0; while(1) { step++; int c=0; for(int i=0;i<n;i++) { s12[c++]=s2[i]; s12[c++]=s1[i]; } s12[c]='\0'; if(strcmp(s12,ss)==0) { flag=1; //printf("%d\n",step); break; } if(vis[s12]==1) { //printf("-1\n"); break; } vis[s12]=1; for(int i=0;i<n;i++) { s1[i]=s12[i]; } c=0; for(int i=n;i<2*n;i++) { s2[c++]=s12[i]; } s2[c]='\0'; } if(flag==1) printf("%d %d\n",cou,step); else printf("%d -1\n",cou); } return 0; }
相关文章推荐
- POJ 3087 Shuffle'm Up (简单模拟)
- poj3087--Shuffle'm Up(小模拟)
- POJ3087-Shuffle'm Up 简单的bfs模拟题
- STL学习(二)set、map、list、deque简单Demo
- 有时候 如果你遇到一个不懂 没做过的功能 可以先新建一个解决方案 最近简单模拟一下
- Visual Studio 2010已经发布,简单列举一下新特性,重点学习一下
- Silverlight学习笔记一(理解一下机制,使用一下布局,实现一个简单的用户登录)
- Java中map的学习(说明一下java中map、迭代器Iterator的用法)
- spring学习笔记6--简单模拟实现AOP(cglib版)
- Spring学习笔记--- BeanFactory简单模拟
- POJ3087 Shuffle'm Up
- poj3087 Shuffle'm Up 骗子,这根本不是宽搜,这就是模拟
- WinForm学习 --简单的模拟时钟程序
- 简单n! (本来只是一个·简单的地推,只是大数问题有点纠结·,本人用数组简单模拟了一下)
- Bing Maps 辅助工具 —— MapCruncher 简单使用学习
- 无监督特征学习:模拟简单细胞、复杂细胞属性的特征(上段工作总结)
- 系统地学习ASP.NET AJAX(1) - 简单地过一下每个控件
- 工作之余,整理了一下平时处理数据的一些常用的,简单的方法。希望对学习JAVA的朋友有点帮助
- 03-22 简单测试了一下map和列表解析的速度对比
- POJ3087《Shuffle'm Up》方法:模拟