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记录该字符串是否被搜过.读懂题目是关键. */ ...
随机推荐
- jquery选择器大全参考
在Dom 编程中我们只能使用有限的函数根据id 或者TagName 获取Dom 对象. 然而在jQuery 中则完全不同,jQuery 提供了异常强大的选择器用来帮助我们获取页面上的对象, 并且将对象 ...
- 禁止Asp.Net WebService 的Test页面功能
只需要Web.Config里面添加: <system.web> <webServices> <protocols> <remove name="Ht ...
- 深入理解jvm jdk1,7(1)
java 虚拟机管理的内存模型包含以下几个运行时数据区域: 程序计数器: 程序计数器是一块较小的内存空间,它可以看成当前线程执行的字节码的行号指示器.在虚拟机的概念模型里(仅是概念模型,各种虚拟机可能 ...
- 1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛
1269 匈牙利游戏 2012年CCC加拿大高中生信息学奥赛 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description ...
- javascript数组属性及方法
数组元素的添加 1. arrayt.splice(index,howmany,item1,.....,itemX) 向/从数组中添加/删除项目,然后返回被删除的项目 2. array.unshift( ...
- use scanner/smb/smb_version
use scanner/smb/smb_version msf auxiliary(smb_version) > set RHOSTS 172.16.21.170RHOSTS => 172 ...
- 调用cmd命令行命令(借鉴)
留待以后观看 ———————————————————————————————————————————————————————————————————————————— public class IP_ ...
- 人工智能背景下的 Office 365 现状和发展趋势
谈论人工智能是让人兴奋的,因为它具有让人兴奋的两大特征 —— 每个人都似乎知道一点并且以知道一点为荣,但又好像没多少人能真正讲的明白.毫无疑问,我也仅仅是知道一点点,这一篇文章试图想通过比较接地气的方 ...
- X86/X64 函数调用约定
C 语言有 __cdecl.__stdcall.__fastcall.naked.__pascal. C++ 语言有 __cdecl.__stdcall.__fastcall.naked.__pasc ...
- IOS 导航控制器基本使用(UINavigationController代码创建方式)
● UINavigationController的使用步骤 ➢ 初始化UINavigationController ➢ 设置UIWindow的rootViewController为UINavigati ...
