题目链接:http://poj.org/problem?id=3087

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
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC

Sample Output

1 2
2 -1

题意:

给出两堆同样数量的筹码 $S_1$ 和 $S_2$。洗筹码的方式是,将 $S_1$ 和 $S_2$ 互相间隔插入,同时 $S_1$ 靠上,$S_2$ 靠下。

然后将新生成的筹码堆的下面一半作为新的 $S_1$,上面一半作为新的 $S_2$。不断循环如上操作,问是否能达到题目所给的目标结果。

题解:

这个在kuangbin的专题里放进了搜索里面,然而感觉每次洗筹码生成的结果都是唯一的,所以不就是一直洗牌直到能达到目标为止吗?感觉并不是搜索啊……

洗牌如果洗了一个循环,发现并不能达到目标的话,就是Impossible了。

AC代码:

#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
typedef pair<string,string> pss; int n;
pss st,ed;
void nxt(pss &x)
{
string s;
for(int i=;i<n;i++) s+=x.second[i], s+=x.first[i];
x.first=s.substr(,n);
x.second=s.substr(n,n);
}
int main()
{
int T;
cin>>T;
for(int kase=;kase<=T;kase++)
{
scanf("%d",&n);
cin>>st.first>>st.second;
cin>>ed.first;
ed.second=ed.first.substr(n,n);
ed.first.erase(n,n);
int cnt;
pss now;
for(cnt=,now=st;;cnt++,nxt(now))
{
if(now==st && cnt>)
{
printf("%d -1\n",kase);
break;
}
if(now==ed)
{
printf("%d %d\n",kase,cnt);
break;
}
}
}
}

POJ 3078 - Shuffle'm Up - [模拟题]的更多相关文章

  1. poj 1008:Maya Calendar(模拟题,玛雅日历转换)

    Maya Calendar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64795   Accepted: 19978 D ...

  2. poj 3087 Shuffle'm Up (模拟过程)

    Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...

  3. POJ 3087 Shuffle'm Up (模拟+map)

    题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...

  4. poj 1028 Web Navigation 【模拟题】

    题目地址:http://poj.org/problem?id=1028 测试样例: Sample Input VISIT http://acm.ashland.edu/ VISIT http://ac ...

  5. POJ 3087 Shuffle'm Up 模拟,看着不像搜索啊

    题意:给定s1,s1两副扑克,顺序从下到上.依次将s2,s1的扑克一张一张混合.例如s1,ABC; s2,DEF. 则第一次混合后为DAEBFC. 然后令前半段为s1, 后半段为s2. 如果可以变换成 ...

  6. poj 1573 Robot Motion【模拟题 写个while循环一直到机器人跳出来】

                                                                                                         ...

  7. POJ 3393 Lucky and Good Months by Gregorian Calendar 模拟题

    题目:http://poj.org/problem?id=3393 不多说了,简单模拟题,因为粗心写错了两个字母,导致错了N遍,模拟还是一贯的恶心,代码实在不想优化了,写的难看了点.. #includ ...

  8. POJ 模拟题集合

    http://www.cppblog.com/Uriel/articles/101592.html 感觉这个暑假没有去年有激情啊,,,还没到状态就已经块上学了,,, 真是弱暴了,,,找几道模拟题刷刷. ...

  9. POJ 3923 Ugly Windows(——考察思维缜密性的模拟题)

    题目链接: http://poj.org/problem?id=3923 题意描述: 输入一个n*m的屏幕 该屏幕内有至少一个对话框(每个对话框都有对应的字母表示) 判断并输出该屏幕内处于最表层的对话 ...

随机推荐

  1. Java 基础【18】 反射与内省

    1.概念定义 Java 反射机制(Reflect)容许程序在运行时加载.探知.使用编译期间完全未知的 class,核心类 java.lang.Class. 通过把指定类中各种元素映射成 java.la ...

  2. 《Java 9 揭秘》全目录汇总

    Tips 做一个终身学习的人. 当写这篇文章时,关于Java 9的学习就先告一段落了. 首先介绍一下背景,大概两个月前,我突然有兴趣想看看Java 9,当时读了一本英文原著<Java 9 Rev ...

  3. Java 多线程重排序的探究

    最近在看<Java 并发编程实战>,之前对所谓的线程的重排序一脸懵逼,今天终于有了点理解. 我们先来看下这个实例Demo /** * 线程的重排序问题 * **/ public class ...

  4. Sublime Text 输入法跟随光标

    通过PackageControl安装“IMESupport”,重启Sublime Text3,即可解决:  注:如项目自述,仅支持Windows.

  5. Shell 实现多线程(多任务)

    实现方案: 1.命令结尾添加:& #/bin/bash all_num= a=$(date +%H%M%S) ${all_num}` do { echo ${num} } & done ...

  6. nlp资料网站

    原文地址 http://blog.sina.com.cn/s/blog_574a437f01019poo.html 昨天实验室一位刚进组的同学发邮件来问我如何查找学术论文,这让我想起自己刚读研究生时茫 ...

  7. C语言 · 滑动解锁

    题目:滑动解锁 滑动解锁是智能手机一项常用的功能.你需要在3x3的点阵上,从任意一个点开始,反复移动到一个尚未经过的"相邻"的点.这些划过的点所组成的有向折线,如果与预设的折线在图 ...

  8. oozie调度sqoop Job 数据库密码无法保存

    问题描述 通过oozie调度sqoop作业时,需要输入数据库作业密码,但在sqoop元数据服务配置密码后,过一段时间会失效. 解决方法 将数据库密码写入HDFS文件,通过配置Sqoop job,实现传 ...

  9. a,abbr,address,area,article, aside, audio标签文档

    <a>标签 download属性 <!-- 下载hello.txt --> <a href="test.txt" download="hel ...

  10. Android Studio 3依赖配置

    新配置 对应的过时配置 描述 implementation compile module编译时可用,module的使用者运行时可用,对于大量使用library的项目,可以显著提高编译时间,因为它可以减 ...