POJ3087 Shuffle'm Up —— 打表找规律 / map判重
题目链接:http://poj.org/problem?id=3087
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12305 | Accepted: 5708 |
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
Source
题解:
方法一:经打表发现,当经过2n次洗牌之后,必定能回到初始状态,所以直接枚举到2n。 问:为何经过2*n后必定能回到初始状态?自己也不会证明,但有一点:对于一个数来说,它所在的位置只有2n种情况。应该与这个有关。复杂度计算:1000(测试组数)*200(2n)*200(字符串长度) = 4e7,如果真的是这个计算次数的话,是会超时的,但可能数据比较弱。
方法二:直接模拟,然后加个map判重,如果某个状态出现过,并且不是目标状态,则可以得出结论:不可能得到目标状态。因为洗牌的状态是会不断循环的,当发现一个状态已经出现过,则表明之前的那些状态构成一个循环,同时也是所有状态的集合,所以不可能得到目标状态。复杂度计算:1000(测试组数)*200(2n)*log200(map判重)*200(字符串长度) = 4e7 * log200, 跟方法一差不了多少(log200很小了)。
代码一:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char a[MAXN], b[MAXN], c[MAXN], aim[MAXN]; int main()
{
int T, n;
scanf("%d",&T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d",&n);
scanf("%s%s%s", a, b, aim);
int ans = -;
for(int k = ; k<=*n; k++)
{
int cnt = ;
for(int i = ; i<n; i++)
{
c[cnt++] = b[i];
c[cnt++] = a[i];
}
c[cnt] = '\0';
if(strcmp(c, aim)==)
{
ans = k;
break;
}
for(int i = ; i<n; i++)
{
a[i] = c[i];
b[i] = c[n+i];
}
}
printf("%d %d\n",kase, ans);
}
}
代码二:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char a[MAXN], b[MAXN], c[MAXN], aim[MAXN];
map<string, bool>exist; int main()
{
int T, n;
scanf("%d",&T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d",&n);
scanf("%s%s%s", a, b, aim);
exist.clear();
int ans;
for(int k = ; true; k++)
{
int cnt = ;
for(int i = ; i<n; i++)
{
c[cnt++] = b[i];
c[cnt++] = a[i];
}
c[cnt] = '\0';
if(strcmp(c, aim)==)
{
ans = k;
break;
}
else if(exist[c])
{
ans = -;
break;
}
exist[c] = true;
for(int i = ; i<n; i++)
{
a[i] = c[i];
b[i] = c[n+i];
}
}
printf("%d %d\n",kase, ans);
}
}
POJ3087 Shuffle'm Up —— 打表找规律 / map判重的更多相关文章
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- HDU 5753 Permutation Bo (推导 or 打表找规律)
Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- HDU 4861 Couple doubi (数论 or 打表找规律)
Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...
- HDU2149-Good Luck in CET-4 Everybody!(博弈,打表找规律)
Good Luck in CET-4 Everybody! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 【ZOJ】3785 What day is that day? ——浅谈KMP在ACM竞赛中的暴力打表找规律中的应用
转载请声明出处:http://www.cnblogs.com/kevince/p/3887827.html ——By Kevince 首先声明一下,这里的规律指的是循环,即找到最小循环周期. 这 ...
- HDU 5795 A Simple Nim(SG打表找规律)
SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...
- hdu_5894_hannnnah_j’s Biological Test(打表找规律)
题目链接:hdu_5894_hannnnah_j’s Biological Test 题意: 有n个不同的位置围成一个圈,现在要安排m个人坐,每个人至少的间隔为k,问有多少种安排 题解: 先打表找规律 ...
- hdu_5795_A Simple Nim(打表找规律的博弈)
题目链接:hdu_5795_A Simple Nim 题意: 有N堆石子,你可以取每堆的1-m个,也可以将这堆石子分成3堆,问你先手输还是赢 题解: 打表找规律可得: sg[0]=0 当x=8k+7时 ...
- hdu_5793_A Boring Question(打表找规律)
题目链接:hdu_5793_A Boring Question 题意: 自己看吧,说不清楚了. 题解: 打表找规律 #include<cstdio> typedef long long l ...
随机推荐
- out.print和out.write
这是一个JSP页面: <%@ page language="java" import="java.util.*" %> <%@ page p ...
- angular父子scope之间的访问
1.子可以访问父的scope,也可以更新相同的scope,但父scope不会被刷新 2.父要访问子scope的方法
- Lucene 6.5.0 入门Demo
Lucene 6.5.0 要求jdk 1.8 1.目录结构: 2.数据库环境: private int id; private String name; private float price; pr ...
- 测试开发系列之Python开发mock接口(三)
于进入主题了,前面的准备工作都已经做好了,下面就开始写逻辑的代码了,代码我已经写好了,每行都加了注释,不明白的可以留言. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ...
- LeetCode:926. 将字符串翻转到单调递增
暴力法超时:思想:动态规划 public int minFlipsMonoIncrb(String S) { int result = S.length(); for (int i = 0; i &l ...
- Go --- GC优化经验
不想看长篇大论的,这里先给个结论,go的gc还不完善但也不算不靠谱,关键看怎么用,尽量不要创建大量对象,也尽量不要频繁创建对象,这个道理其实在所有带gc的编程语言也都通用. 想知道如何提前预防和解决问 ...
- git-flow 工作流 备忘清单
关于 git-flow 是一个 git 扩展集,按 Vincent Driessen 的分支模型提供高层次的库操作. 查看详情 ★ ★ ★ 这个备忘清单展示了 git-flow 的基本操作和效果. ★ ...
- php编译安装后,加扩展模块
1.进入php源码包中,找到需要安装的扩展模块目录. cd /root/php-5.6.26/ext/mbstring 2.在扩展模块目录,运行phpize程序. /usr/local/bin/php ...
- 用C++实现一个Log系统
提要 近期在写一些C++的图形代码,在调试和測试过程中都会须要在终端打印一些信息出来. 之前的做法是直接用 std::cout<<"Some Word"<< ...
- Java 实现 淘宝秒杀 聚划算 自己主动提醒 源代码
说明 本实例可以监控聚划算的抢购button,在聚划算整点聚的时间到达时自己主动弹开页面(URL自定义). 能够自己定义监控持续分钟数,同一时候还能够通过多线程加快刷新速度. 源代码 package ...