POJ 3087 Shuffle'm Up【模拟/map/string】
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<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#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<int,int> 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<string,int> 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<n; i++)
{
s3 = s3 + s2[i] + s1[i];
}
if(mp[s3])
{
ans=-1;
break;
}
else if(s3==s)
{
ans=t;
break;
}
else
{
mp[s3]=1;
s1=s3.substr(0,n);
s2=s3.substr(n,n);
}
}
cout<<cas++<<' '<<ans<<endl;
}
}
/*
2
4
AHAH
HAHA
HHAAAAHH
HAAH
3
CDE
CDE
EEDDCC
Sample Output
1 2
2 -1
*/
POJ 3087 Shuffle'm Up【模拟/map/string】的更多相关文章
- POJ 3087 Shuffle'm Up (模拟+map)
题目链接:http://poj.org/problem?id=3087 题目大意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块 ...
- poj 3087 Shuffle'm Up (模拟过程)
Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...
- POJ 3087 Shuffle'm Up 模拟,看着不像搜索啊
题意:给定s1,s1两副扑克,顺序从下到上.依次将s2,s1的扑克一张一张混合.例如s1,ABC; s2,DEF. 则第一次混合后为DAEBFC. 然后令前半段为s1, 后半段为s2. 如果可以变换成 ...
- 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(洗牌)
POJ 3087 Shuffle'm Up(洗牌) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 A common pas ...
- poj 3087 Shuffle'm Up ( map 模拟 )
题目:http://poj.org/problem?id=3087 题意:已知两堆牌s1和s2的初始状态, 其牌数均为c,按给定规则能将他们相互交叉组合成一堆牌s12,再将s12的最底下的c块牌归为s ...
- POJ 3087 Shuffle'm Up(模拟)
Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7404 Accepted: 3421 Desc ...
- POJ 3087 Shuffle'm Up
Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- 关于网站转码(SiteApp转码)
1.Siteapp页面转码的意义?在百度移动搜索引擎中为更好满足用户信息需求,会同时为用户提供pc网页和mobile网页,但目前大多数PC页在移动终端中直接浏览的体验较差(交互.兼容和流量等).因此为 ...
- [IOI2007 D1T1]Miners 矿工配餐
题目大意:有$2$个煤矿,$n$天.每天给一个煤矿送餐(共有有$3$种餐),价值为它与前面两次送餐(如果有的话)不同的种类数.最大化价值. 题解:看到只有三种餐,考虑状压$DP$.$f_{i,j,k, ...
- linux下搭建redis并解决无法连接redis的问题
以前公司在开发阶段连接的redis一直是正式环境中的,最近老大让我在搭建一个局域网内的redis用于开发阶段时连接使用,搭建过程中也遇到了一些问题,还好已经解决了,在这里记录一下. 首先是搭建redi ...
- BZOJ_DAY6???
昨天没睡好啊啊啊,真是要命,睡不着,今天状态爆炸...34题击破. 下一步目标:网络流24题,树链剖分. (洛谷比赛了好开心,希望这次能比以前强吧,嗯)
- Could not resolve com.android.support:multidex:1.0.2
http://blog.csdn.net/goodlixueyong/article/details/50992835
- Join/remove server into/from windows domain PS script
Join server into windows domain PS script $username = "ad-domain\admin" $Password = " ...
- Kafka自我学习1-Multi-broker cluster
====================================Testing environment =========================================== ...
- node安装
由于Node.js平台是在后端运行JavaScript代码,所以,必须首先在本机安装Node环境. 安装Node.js 目前Node.js的最新版本是7.6.x.首先,从Node.js官网下载对应平台 ...
- svn“Previous operation has not finished; run 'cleanup' if it was interrupted“ 或者不能cleanup,或者提示空目录 报错的解决方法
参考了文档: http://blog.csdn.net/superch0054/article/details/38668017 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Prev ...
- Http/2 升级指南
[转]http://www.syyong.com/architecture/http2.html HTTP/2(最初名为HTTP/2.0)是 WWW 使用的 HTTP 网络协议的主要版本. 它来自早先 ...