博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3087 Shuffle'm Up【模拟/map/string】
阅读量:6975 次
发布时间:2019-06-27

本文共 4238 字,大约阅读时间需要 14 分钟。

Shuffle'm Up

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14471 Accepted: 6633
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

Greater New York 2006

【题意】:将s2的先放,再放s1,得到一个新字符串,截取前半部分给s1,后半部分给s2,重复上述操作,若能得到给定字符串s,输出操作次数,若不能输出-1
【分析】:map记录出现过的合成字符串,若有重复的,一定无法输出给定的字符串。
【代码】:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define debug() puts("++++")#define gcd(a,b) __gcd(a,b)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define fi first#define se second#define pb push_back#define sqr(x) ((x)*(x))#define ms(a,b) memset(a,b,sizeof(a))#define sz size()#define be begin()#define pu push_up#define pd push_down#define cl clear()#define lowbit(x) -x&x#define all 1,n,1#define rep(i,n,x) for(int i=(x); i<(n); i++)#define in freopen("in.in","r",stdin)#define out freopen("out.out","w",stdout)using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair
P;const int INF = 0x3f3f3f3f;const LL LNF = 1e18;const int maxn = 1e3 + 20;const int maxm = 1e6 + 10;const int N = 1e4+10;const double PI = acos(-1.0);const double eps = 1e-8;const int dx[] = {-1,1,0,0,1,1,-1,-1};const int dy[] = {0,0,1,-1,1,-1,1,-1};int dir[][3]={ {0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0} };const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int t,n;string s1,s2,s3,s;map
mp;int main(){ scanf("%d",&t); int cas=1; while(t--) { int t=0,ans=0; scanf("%d",&n); cin>>s1>>s2>>s; while(1) { s3=""; t++; for(int i=0; i

转载于:https://www.cnblogs.com/Roni-i/p/9281162.html

你可能感兴趣的文章
Unity3D 材料
查看>>
ControlButton按钮事件
查看>>
HTTP 缓存
查看>>
Apache2.4+Tomcat7集群搭建
查看>>
Linux内置的审计跟踪工具:last命令
查看>>
Nginx自定义模块编写:根据post参数路由到不同服务器
查看>>
Lamp源码安装
查看>>
Linux0.00内核为什么要自己设置0x80号陷阱门来调用write_char过程?
查看>>
mysql数据库备份、恢复文档
查看>>
在linux上MySQL的三种安装方式
查看>>
cocos2dx 场景的切换
查看>>
Java用for循环Map
查看>>
让你提升命令行效率的 Bash 快捷键
查看>>
Python运维项目中用到的redis经验及数据类型
查看>>
一些要注意的地方
查看>>
android Spinner 例子
查看>>
2013年10月1日C#随机数
查看>>
fastJson结合Nutz.Mapl的进阶应用
查看>>
使用react心得
查看>>
大一新生,你为何逃课?
查看>>