【题目描述】

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

【题目分析】
感觉这道题并不是经典的搜索,完全是一个模拟,也没有多种情况,在网上看其他人的博客好像都是用的BFS或者是DFS,觉得不必要
关键还是判断无法洗出那样情况的判断,发现用set很方便,将所有出现过的情况都加入到set里面,一旦重复就说明洗不出来了(这个时候还没有出现想要洗出的牌,那么继续洗下去只会一直重复循环)【AC代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<climits>
#include<set>using namespace std;int n,ans;
string s1,s2,s12,s;string cat(const string& s1,const string& s2)
{string cnt="";for(int i=0;i<n;i++){cnt.push_back(s2[i]);cnt.push_back(s1[i]);}return cnt;
}bool work()
{string a,b;set<string> st;ans=1;s=cat(s1,s2);st.insert(s);while(1){//cout<<s<<" "<<s12<<endl;if(s==s12){return true;}a=s.substr(0,n);b=s.substr(n,2*n);s=cat(a,b);ans++;if(st.find(s)!=st.end())return false;st.insert(s);}
}int main()
{//ios::sync_with_stdio(false);//发现vjudge上用不了这个优化,不知道为什么int T;scanf("%d",&T);for(int V=1;V<=T;V++){scanf("%d",&n);cin>>s1>>s2>>s12;if(work()){printf("%d %d\n",V,ans);}else{printf("%d -1\n",V);}}return 0;
}

Shuffle'm Up——简单模拟相关推荐

  1. 用Python简单模拟《原神》抽卡系统

    用Python简单模拟<原神>抽卡系统[抽卡模拟器] 简介 代码思想 保底机制 概率 概率公式 代码构建 导入软件包random和os 初始化概率 增加概率 保底机制 创建文件夹 抽卡次数 ...

  2. IoC容器总结与简单模拟

    IoC容器总结与简单模拟 当一个组件需要外部资源时,最直接也最明智的方法是执行查找,这种行为称为主动查找.但这种查找存在一个缺点--组件需要知道如何获得资源.那么它的解决方案是什么呢?请看下文. AD ...

  3. 使用动态代理简单模拟一下spring的事务管理

    按照平时写代码的习惯,我们会定义一个service接口 package com.proxy.test; public interface UserService {public void sayHel ...

  4. 7-18 银行业务队列简单模拟 (25 分)

    7-18 银行业务队列简单模拟 (25 分) 设某银行有A.B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 -- 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客.给定到达 ...

  5. Jmeter简介以及简单模拟性能测试

    1.Jemter简介 1.我们为什么使用Jmeter 开源,免费,基于Java编写,可集成到其他系统可拓展各个功能插件 支持接口测试, 压力(负载和压力)测试等多种功能,支持录制回放, 入门简单相较于 ...

  6. JavaWeb学习总结(四十九)——简单模拟Sping MVC

    在Spring MVC中,将一个普通的java类标注上Controller注解之后,再将类中的方法使用RequestMapping注解标注,那么这个普通的java类就够处理Web请求,示例代码如下: ...

  7. 银行业务队列简单模拟 (25 分)c语言c++

    7-2 银行业务队列简单模拟 (25 分) 设某银行有A.B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 -- 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客.给定到达银 ...

  8. NYOJ 题目77 开灯问题(简单模拟)

    开灯问题 时间限制:3000 ms  |            内存限制:65535 KB 难度:1 描述 有n盏灯,编号为1~n,第1个人把所有灯打开,第2个人按下所有编号为2 的倍数的开关(这些灯 ...

  9. FZU - 2202 犯罪嫌疑人(逻辑思维+简单模拟)

    题目链接:点击查看 题目大意:给出n和m,代表有n个人,每个人说一句话,指认一个人是无辜还是罪犯,总共有m个人说了真话,问每个人说话的真实性 题目分析:一拿到这个题目我是懵逼的..因为n给到了1e5, ...

最新文章

  1. MongoDB sharding迁移那些事(一)
  2. leetcode算法题--设计链表
  3. linux提高nand速度,linux-2.6.31.1内核支持Nand Flash
  4. sql 问号的使用 php_PHP中bindParam和bindValue的区别
  5. ASP.NET三层架构之不确定查询参数个数的查询
  6. 在Java 8之前,您编写了几行代码来对对象集合进行排序?
  7. [Linux] VIM 代码折叠
  8. java boolean if_Java if(boolean)和if(boolean=true)区别解析
  9. Unity的Json解析二–写Json文件
  10. 【C语言】单词个数统计(库函数第一次运用)
  11. Python连接两个字符串并去除首尾重复子串
  12. win10 mfc 连接mysql_win10下使用c语言连接mysql
  13. 无线通信基础(二):高斯噪声中的检测
  14. 没有安装python如何运行py_在没有安装Python的前提下,让Sublime text编辑器来运行Py?...
  15. 640-802 新版CCNA考试题库下载
  16. RTI DDS windows环境下的下载和安装
  17. 网站的服务器ip变动,网站切换服务器IP,如何快速快速刷新DNS以获得测试?
  18. android 涨潮动画加载_潮汐app下载 潮汐 (睡眠白噪音番茄钟) for Android V3.9.1 安卓手机版 下载-脚本之家...
  19. yl335b分拣站单元流程图_基于PLC与MCGS的YL-335B分拣单元的创新设计
  20. EIA 标准电阻速查表

热门文章

  1. property修饰关键字
  2. tensorflow mnist read_data_sets fails
  3. oj运行时错误如何查找原因_VLOOKUP又失灵?避免这四种错误类型
  4. linux mono mysql_LJMM平台( Linux +Jexus+MySQL+mono) 上使用MySQL的简单总结
  5. activiti api文档_【白银人机】Activiti 工作流从入门到入土:完整 hello world 大比拼(API 结合实例讲解)...
  6. php如何导入数据,““php中如何将execl的数据导入到数据库中
  7. mysql workbench启动_怎么启动mysql workbench
  8. python下载显示文件丢失_Microsoft.PythonTools.resources.dll
  9. numpy维度交换_“lazy”的transpose()函数——从numpy 数组的内存布局讲起
  10. java type 类型,java中的泛型类型与Type接口