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 ...
随机推荐
- PHP提示Cannot modify header information - headers already sent by解决方法
PHP提示Cannot modify header information - headers already sent by解决方法 因为 header();发送头之前不能有任何输出,空格也不行, ...
- Struts2的值栈和OGNL牛逼啊
Struts2的值栈和OGNL牛逼啊 一 值栈简介: 值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务. 二 ...
- Codeforces Gym 100418K Cards 组合数学
CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...
- db2 获取自增主键的方法
1.用SEQUENCES方式 建表语句 CREATE TABLE TEST1( PKEY INTEGER NOT NULL, NAME VARCHAR(100), SEX VARCHAR(100), ...
- Java下接口interface前面要不要加I
说明:加I和不加I都可以,看需要,没有强制要求. 在Java中更多是提倡不加I的,可以看下JDK的源码,都是不加I的. 微软C#是规定要加I,这也是影响从而导致有这个话题的原因. Java中特定不直接 ...
- java内部类理解使用
这是我学习Java内部类的笔记 1.为什么使用内部类?使用内部类最吸引人的原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响1.1 ...
- Win10激活Office2013的技巧
原文:http://www.xitongzhijia.net/xtjc/20150720/53252.html KMSpico Win10激活工具 是一款能激活Win8/Win8.1/win10/Of ...
- php 解决MySQL插入数据出现 Incorrect string value: '\xF0\x9F\x92\x8BTi...'错误
在项目中向MySQL插入数据时.发现数据插入不完整,通过调试,发现插入语句也没什么特殊的错误. 可是就是差不进去,于是就打开mysqli错误的调试 $ret = mysqli_query($this- ...
- jquery提示消息,简单通用
jquery提示消息.简单通用 function showTips(txt,time,status) { var htmlCon = ''; if(txt != ''){ if(status != 0 ...
- 递归获取JSON内容的key-value值
方法主体: 使用时,请在类中先声明一个Map,參数形式例如以下: JSONObject jobj = new JSONObject(JSONContent); 首次请传递jobj.