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
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
Sample Output
1 2
2 -1 题目分析:按题意模拟即可。 代码如下:
# include<iostream>
# include<cstdio>
# include<map>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std;
string unit(string s1,string s2)
{
string res="";
int i=,j=,l=s1.size();
for(int k=;k<*l;++k){
if(k&)
res+=s1[i++];
else
res+=s2[j++];
}
return res;
}
void solve(string s1,string s2,string S,int l)
{
map<string,int>mp;
int cnt=;
string s=unit(s1,s2);
while(){
++mp[s];
++cnt;
if(s==S){
printf("%d\n",cnt);
return ;
}
s1=s.substr(,l);
s2=s.substr(l,l);
s=unit(s1,s2);
if(mp[s]>=)
break;
}
printf("-1\n");
}
int main()
{
int T,l,cas=;
string s1,s2,s;
scanf("%d\n",&T);
while(T--)
{
cin>>l;
cin>>s1>>s2>>s;
printf("%d ",++cas);
solve(s1,s2,s,l);
}
return ;
}
POJ-3087 Shuffle'm Up (模拟)的更多相关文章
- POJ 3087 Shuffle'm Up (模拟+map)
题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...
- poj 3087 Shuffle'm Up (模拟过程)
Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...
- POJ 3087 Shuffle'm Up 模拟,看着不像搜索啊
题意:给定s1,s1两副扑克,顺序从下到上.依次将s2,s1的扑克一张一张混合.例如s1,ABC; s2,DEF. 则第一次混合后为DAEBFC. 然后令前半段为s1, 后半段为s2. 如果可以变换成 ...
- POJ.3087 Shuffle'm Up (模拟)
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
- 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记录该字符串是否被搜过.读懂题目是关键. */ ...
- 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 ( map 模拟 )
题目:http://poj.org/problem?id=3087 题意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s ...
- POJ 3087 Shuffle'm Up(模拟)
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7404 Accepted: 3421 Desc ...
- POJ 3087 Shuffle'm Up【模拟/map/string】
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14471 Accepted: 6633 Descrip ...
随机推荐
- 冒泡排序法原理讲解及PHP代码示例
冒泡排序原理 冒泡排序对一个数组里的数字进行排序,把数组里两个相邻的数比较大小,将值小的数放在前面,把大的数往后面放,当然这种排序是升序,即从小到大.举例说明$array = [64, 56, 31, ...
- 20145206邹京儒《网络对抗技术》 PC平台逆向破解
20145206邹京儒<网络对抗技术> PC平台逆向破解 注入shellcode并执行 一.准备一段shellcode 二.设置环境 具体在终端中输入如下: apt-cache searc ...
- Go第一篇之轻松入门
Go语言简介 Go语言简史 Go 语言(或 Golang)是 Google 在 2007 年开发的一种开源编程语言,于 2009 年 11 月 10 日向全球公布.Go 是非常年轻的一门语言,它的主要 ...
- JavaScript:Delete属性
以前,我就晓得delete只能够删除隐性属性(就是没有进行声明的变量),但是不知道为什么这样? 隐性属性:在页面中以前没有声明过该变量,直接进行赋值的 str='hongda' 其实这是由属性的特性决 ...
- 垒骰子|2015年蓝桥杯B组题解析第九题-fishers
垒骰子 赌圣atm晚年迷恋上了垒骰子,就是把骰子一个垒在另一个上边,不能歪歪扭扭,要垒成方柱体. 经过长期观察,atm 发现了稳定骰子的奥秘:有些数字的面贴着会互相排斥! 我们先来规范一下骰子:1 的 ...
- C#中dll附加配置文件
DLL-with-configuration-file带配置文件的dll http://www.codeproject.com/Tips/199441/DLL-with-configuration-f ...
- [Shiro] - shiro之SSM中的使用
在学习shiro的途中,在github发现了一个开源项目,所需的控件刚好是自己要学习的方向. 虽然还要学习完ssm的shiro与springboot的shiro,以及接下来的种种控件和类库,但学习这个 ...
- python 散列表查找
class HashTable: def __init__(self, size): self.elem = [None for i in range(size)] self.count = size ...
- js获取url 参数
window.getRequest = function (url) { var theRequest = new Object(); var indexOf = url.indexOf(" ...
- Codeforces 837D - Round Subset(dp)
837D - Round Subset 思路:dp.0是由2*5产生的. ①dp[i][j]表示选i个数,因子2的个数为j时因子5的个数. 状态转移方程:dp[i][j]=max(dp[i][j],d ...