题目链接: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. 【转】关于免费SSL证书的那些事儿

    根据 Let’s Encrypt CA 的统计,截至 2017 年 11 月,Firefox 加载的网页中启用 HTTPS 的比例占 67%,比去年底的 45% 有巨大提升.浏览器开发商如 Mozil ...

  2. MacOS下保护浏览器主页和默认搜索

    Windows流氓软件已被中国人玩烂了, 终于连Mac也被沦陷. 视频演示地址: http://v.youku.com/v_show/id_XMTMwMTk4MzQ0MA==.html

  3. 使用ansible结合FTP部署zabbix_agent

    想要达到的效果:一条命令,快速对多台主机部署zabbix_agent 实现思路:从源码编译编译出zabbix_agentd,准备好安装脚本,将安装脚本和编译出的agent一起上传到FTP服务器,在an ...

  4. mac关闭占用某个端口的进程

    在启动项目的时候有时候会提示端口被占用,但是怎么都找不到那个关闭进程的地方,可以直接通过命令行关闭这个进程: 比如要关闭:8000端口的进程: 1. 查找端口进程: lsof -i: 会把所有的占用8 ...

  5. 检查linux的磁盘空间占用

    先初步看看哪个目录占用最大$ df -h 然后细看遍历某目录的占用情况:$ sudo du -a /data  | sort -nr | less(单位是KB)

  6. IDEA community + Gradle + Gretty 调试 servlet 应用 + war包部署到tomcat

    参考:https://guides.gradle.org/building-java-web-applications 1.运行和调试 IDEA创建gradle项目,项目结构如下 各个文件: buil ...

  7. jQuery EasyUI 详解

    EasyUI 简介 easyui 是一种基于 jQuery 的用户界面插件集合. easyui 为创建现代化,互动,JavaScript 应用程序,提供必要的功能. 使用 easyui 你不需要写很多 ...

  8. WPF ViewModelLocator

    在WPF中应用ViewModelLocator <Window x:Class="Demo.Views.MainWindow" ... xmlns:prism="h ...

  9. vue+node+mongodb前后端分离博客系统

    感悟 历时两个多月,终于利用工作之余完成了这个项目的1.0版本,为什么要写这个项目?其实基于vuejs+nodejs构建的开源博客系统有很多,但是大多数不支持服务端渲染,也不支持动态标题,只是做到了前 ...

  10. pom.xml文件最详细的讲解

    1 引入额外的jar包 <dependency> <groupId>bitwalker</groupId> <artifactId>UserAgentU ...