(简单) 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.
题目就是洗牌了,按照他的规则来洗,这个过程可以直接模拟,可以发现编号小于等于C(从下往上编号。)就乘以2,大于C的就乘以2然后-1,然后取模,这样的话可以看出洗牌会循环,因为只有2C种编号,对于某个来说第2C+1次一定是之前有过的编号,每一个都会循环的话,那这整个一定也会循环吧,我觉的就是周期为2C。
然后就是一次次洗牌就好了,出现循环的话就算是不可能了。
代码如下:
#include<iostream>
#include<cstring> using namespace std; char End[];
char Sta[];
int Snum[];
int C; void change()
{
for(int i=;i<=*C;++i)
if(Snum[i]<=C)
Snum[i]*=;
else
Snum[i]=(Snum[i]-C)*-;
} bool judge()
{
for(int i=;i<=C*;++i)
if(End[Snum[i]-]!=Sta[i-])
return ; return ;
} void slove()
{
for(int i=;i<=*C;++i)
Snum[i]=i; int ans=; change(); while(Snum[]!=)
{
if(judge())
{
cout<<ans<<endl;
return;
} change();
++ans;
} if(judge())
cout<<ans<<endl;
else
cout<<-<<endl;
} int main()
{
ios::sync_with_stdio(false); int T;
char temp[];
cin>>T; for(int cas=;cas<=T;++cas)
{
cin>>C;
cin>>Sta;
cin>>temp;
strcat(Sta,temp);
cin>>End; cout<<cas<<' ';
slove();
} return ;
}
(简单) POJ 3087 Shuffle'm Up,枚举。的更多相关文章
- POJ 3087 Shuffle'm Up(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- POJ.3087 Shuffle'm Up (模拟)
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
- 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 线性同余,暴力 难度:2
http://poj.org/problem?id=3087 设:s1={A1,A2,A3,...Ac} s2={Ac+1,Ac+2,Ac+3,....A2c} 则 合在一起成为 Ac+1,A1,Ac ...
- poj 3087 Shuffle'm Up ( map 模拟 )
题目:http://poj.org/problem?id=3087 题意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s ...
- POJ 3087 Shuffle'm Up (模拟+map)
题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...
- POJ 3087 Shuffle'm Up DFS
link:http://poj.org/problem?id=3087 题意:给你两串字串(必定偶数长),按照扑克牌那样的洗法(每次从S2堆底中拿第一张,再从S1堆底拿一张放在上面),洗好后的一堆可以 ...
- [暴力搜索] POJ 3087 Shuffle'm Up
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10003 Accepted: 4631 Des ...
随机推荐
- 【dp 背包变形】 poj 1837
#include <cstdio> #include <memory.h> #include<iostream> using namespace std; ][]; ...
- HDU 2255 奔小康赚大钱 KM算法的简单解释
KM算法一般用来寻找二分图的最优匹配. 步骤: 1.初始化可行标杆 2.对新加入的点用匈牙利算法进行判断 3.若无法加入新编,修改可行标杆 4.重复2.3操作直到找到相等子图的完全匹配. 各步骤简述: ...
- iOS 2.0 版本切入点
转载自:http://www.infoq.com/cn/articles/Version_2_0 移动互联网如火如荼,iOS 应用+ Android 应用+ 手机站似乎成了所有互联网公司的标配,你的网 ...
- Android:TextView最小行数设置
我们有时候为了保证TextView必须有一个最小的高度,那么就需要设置这个行数. 因为如果你不设置的话,在measure这个TextView的时候,此时就无法准确的得到一个最小高度.因为设备不同,所以 ...
- 利用apache组件实现文件上传
实现文件上传需要引入: commons-fileupload-1.3.2.jar commons-io-2.5.jar commons-logging-1.2.jar <!DOCTYPE htm ...
- eclipse没有(添加)"Dynamic Web Project"选项的方法【转载】
第一种方法: 你安装的是专门开发java项目的,而Dynamic Web Project 属于J2EE技术,所以你要专门下载一个集成了J2EE插件的Eclipse,(eclipse-jee-heli ...
- 3--OC -- 点语法
3.OC -- 点语法 1.方法名 // 冒号也是属于方法名的一部分 - (void)setAge:(int)age; // 方法名是 setAge: - (void)setAge; // 方法名是 ...
- hdu_5720_Wool(脑洞)
题目连接:hdu_5720_Wool 题意: 给你N个数,然后给你一个区间,问你在这个区间内有多少数可以不与这N个数任选两个出来组成三角形 题解: 这里我还是贴官方的题解算了 考虑三角形三条边a,b, ...
- AJAX(XMLHttpRequest)进行跨域请求方法详解(二)
注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 2,预检请求 预检请求首先需要向另外一个域名的资源发送一个 HT ...
- linux文件分割(将大的日志文件分割成小的)【转载】
linux文件分割(将大的日志文件分割成小的)linux下文件分割可以通过split命令来实现,可以指定按行数分割和安大小分割两种模式.Linux下文件合并可以通过cat命令来实现,非常简单. 在Li ...