题目链接:http://poj.org/problem?id=3087

Shuffle'm Up
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);
}
}

最新文章

  1. Python爬虫小白入门(二)requests库
  2. Bootstrap系列 -- 9. 表格
  3. [C#] 编程控制笔记本蓝牙与外部蓝牙设备通信
  4. The Cow Lexicon
  5. javascript 面向对象整理
  6. 前端性能优化工具--DOM Monster
  7. 使用Block来进行页面间的传值
  8. inline-block 垂直居中
  9. “jquery于each方法和选择”学习笔记
  10. j2e应用相关技术
  11. ●BZOJ 1853 [Scoi2010]幸运数字
  12. Mysql数据库引擎介绍--转载
  13. ubuntu的安装及ubuntu中安装mysql和tomcat
  14. C语言之控制语言:分支和跳转
  15. 多线程下载图片,同步下载http://www.importnew.com/15731.html
  16. Cocos2dx Android环境编译出错:jni/Android.mk: Cannot find module with tag &#39;scripting/lua-bindings&#39; in import path
  17. Java基于opencv—透视变换矫正图像
  18. Qt之实现网络下发配置的半透明友好提示界面
  19. 【Spark】SparkStreaming-如何使用checkpoint
  20. AjaxAnywhere的用法(FORWARD)

热门文章

  1. 填报志愿(codevs 2930)
  2. 看 nova
  3. Phantomjs和Casperjs,后台网页抓取和交互
  4. Laravel 中视图中使用PHP代码
  5. AC日记——[网络流24题]方格取数问题 cogs 734
  6. 快速掌握RabbitMQ(三)——消息确认、持久化、优先级的C#实现
  7. 如何查看stm32固件库版本及MDK和keil uvision的关系
  8. SPOJ MIXTURES 区间dp
  9. luogu P2296 寻找道路
  10. Dfs学习经验(纸上运行理解DFS)【两道题】