Shuffle'm Up 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 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 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 Sample Output 1 2 Source |
题意:先给你两个长度一样的初始状态字符串s1,s2和一个大小为两倍的最终状态的s12,按照s2的第一个先放在s12第一个再依次是s1,s2,s1…,排完后再按照长度前一半是s1新状态,后一半是s2新状态, 重复以上操作,看得到s12是否有和 最初给的最终状态的s12相同的,有输出步数,没有就输出-1.
思路:按照题目的顺序模拟即可,可以将每一个整合后的一个新的S12放进一个map里面,接下去如果模拟到了目标串就输出,如果发现模拟到的一个s12在map中出现过了并且不是和目标串的一样的话,那么这个时候出现了一个循环,无论如何也模拟不出目标串了,就可以输出-1了。注意那个'\0',一开始没有加这个wa了好几发。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define _e exp(1.0)
#define ll long long
const int maxn=1e6+; char a[maxn];
char b[maxn];
char c[maxn];
char goal[maxn];
int main()
{
int t;
scanf("%d",&t);
int ca=;
while(t--)
{
int n;
int step=;
scanf("%d",&n);
scanf("%s%s",a,b);
scanf("%s",goal);
map<string,int>mp;
mp[goal]=;
while()
{ int pos=;
for(int i=;i<n;i++)
{
c[pos++]=b[i];
c[pos++]=a[i];
}
c[pos]='\0';
step++;
// for(int i=0;i<2*n;i++)
// cout<<c[i];
if(strcmp(c,goal)==)
{
cout<<ca++<<" "<<step<<endl;
break;
}
else if(mp[c]== && strcmp(goal,c))
{
cout<<ca++<<" "<<-<<endl;
break;
}
mp[c] = ;
int ii,k;
for(ii=;ii<n;ii++) //分拆出s1与s2
a[ii]=c[ii];
a[ii]='\0'; for(k=;ii<*n;ii++,k++)
b[k]=c[ii];
b[ii]='\0'; }
}
}
Shuffle'm Up POJ - 3087(模拟)的更多相关文章
- POJ - 3087 模拟 [kuangbin带你飞]专题一
模拟洗牌的过程,合并两堆拍的方式:使先取s2,再取s1:分离成两堆的方式:下面C张放到s1,上面C张到s2.当前牌型与第一次相同时,说明不能搜索到答案. AC代码 #include<cstdio ...
- kuangbin专题 专题一 简单搜索 Shuffle'm Up POJ - 3087
题意:(1)有两副颜色多样的扑克牌,(A~H)表示不同颜色,给你两副牌,S1,S2和一副你需要洗出的KEY,S12由S2最底部,S1底部...一直下去,直到洗成S12,就是图片展示的那样.(2)洗好的 ...
- POJ 3087 模拟
给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到变换次数 变换规则如下: 假设s1=12345,s2=67890 ...
- POJ 3087 模拟+hash
也可以用map来搞 样例推出来 就没啥问题了 (先读的是B 然后是A 被坑好久) //By SiriusRen #include <cstdio> #include <iostrea ...
- POJ.3087 Shuffle'm Up (模拟)
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
- 【模拟】POJ 3087
直达–>POJ 3087 Shuffle'm Up 题意:一开始没怎么看明白,注意现是从S2里拿牌放在最底下,再放S1,这样交叉放(我一开始以为是S1和S2随意哪个先放,分别模拟取最小),然后在 ...
- POJ 3087 Shuffle'm Up
Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3087 Shuffle'm Up(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- DFS POJ 3087 Shuffle'm Up
题目传送门 /* 题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子 DFS:直接模拟搜索,用map记录该字符串是否被搜过.读懂题目是关键. */ ...
随机推荐
- centos6安装bochs
安装包 bochs 2.6.8 平台 centos6 前提依赖 yum groupinstall -y "Server Platform Development" "De ...
- Sublime常用插件安装大全
作为前端人员,要找一个很顺手的编辑器真的不容易,我向大家推荐的一款实用前端开发神器,不但占地小,且插件很多,很强大. 下面我向大家介绍一下它的安装及插件的使用方法. 一.安装及安装emmet插件 首先 ...
- Django的Serializers的使用
Serializer 在这里通过一个验证用户身份的例子说明rest_framework中serializer.Serialize的使用. 编写serializer Serializer的使用不需要依赖 ...
- JNI教程
一.什么是JNI JNI(Java Native Interface ),它是Java SDK的一部分,主要用于实现Java对其他语言编写的代码和库的调用,比如C和C++.JNI提供的API也能让JV ...
- c语言数组赋值
int acct_parm[3] = {4, 2, 30};#define RESUME (acct_parm[0])
- hihoCoder #1044 : 状态压缩·一 (清垃圾)
题意: 某车厢有一列座位,共有n个位置,清洁工要在这n个位置上清垃圾,但是不能全部位置都清理,只能选择部分.选择的规则是,连续的m个位置内,不能够清理超过q个,也就是说从第1~m个位置最多可以清q个, ...
- sublime相关小技巧
1.快速建立一个新文件:Ctrl+n 2.修改多个相同符号:Ctrl+D 3.建立语言后缀的文件保存,例如我想创建PHP的语言脚本,先按Ctrl+Shift+p,打开Command Palette,输 ...
- 联动选择通过ajax获取选择对应的数据
网站有时候需要这种联动然后获取到想对应的数据 思路: 这种的话就是你每次选择哪一个就将这个设置一个标注 表示你现在选择的是哪一个 然后每选择一次就进行一次ajax查询,ajax里面有一个data里面添 ...
- pta 编程题10 Root of AVL Tree
其它pta数据结构编程题请参见:pta 这道题考察平衡二叉查找树的插入. 为了保证二叉查找树的平衡,当一个结点的左右子树的高度差大于1时就要进行调整. 分为以下四种情况: 插入新节点后,以及旋转之后, ...
- Hybris UI的Route(路由)实现
登录Hybris前台,在product catalog里选择Digital camera: 点击某个产品进入明细页面: 注意产品明细这个url: 这个明细页面的路由和SAP UI5的路由思路很像. 在 ...