POJ3087 Shuffle'm Up【簡單模擬】
Shuffle'm Up
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15665 | Accepted: 7143 |
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 (Athrough 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
問題連結:POJ3087 Shuffle'm Up
問題描述:給定s1,s2兩副撲克,順序從下到上。依次將s2,s1的撲克一張一張混合。例如s1=ABC; s2=DEF. 則第一次混合後為DAEBFC。 然後令前半段為s1, 後半段為s2。對於每個測試樣例,有兩個輸出x,y。x表示第幾組測試,y:如果可以變換成所給出的字串,輸出變換次數即可;否則,輸出-1。
解題思路:簡單模擬,關鍵是怎樣判斷不能變換成目標字串,可以使用集合set儲存已經變換得到的字串,如果再次出現說明繼續轉換也不能成功。
AC的C++程式:
#include<iostream>
#include<string>
#include<set>
using namespace std;
int main()
{
int T,len;
cin>>T;
for(int t=1;t<=T;t++){
string s1,s2,s,p;
set<string>S;//儲存已經出現過的字串
cin>>len>>s1>>s2>>s;
int count=0;
cout<<t<<" ";
for(;;){
//進行一次轉換
p=s1+s2;
for(int i=0;i<len;i++){
p[i*2+1]=s1[i];
p[i*2]=s2[i];
}
count++;//轉換的次數加一
if(p==s){//成功,輸出次數並退出
cout<<count<<endl;
break;
}
else if(S.count(p)){//失敗,而且p已經出現過了,繼續轉換也不能成功
cout<<"-1"<<endl;
break;
}
else{//失敗,但是轉換的字串還未出現過,就加入S中,並使更新s1和s2的值
S.insert(p);
s1=p.substr(0,len);
s2=p.substr(len,len);
}
}
}
return 0;
}
相關文章
- 簡單模擬死鎖
- 1046 Shortest Distance(簡單模擬)
- 簡單的模擬(洛谷)
- sort回撥的簡單模擬
- 模擬簡單的動態代理
- 簡單易用的前端模擬資料前端
- 簡單實現.NET Hook與事件模擬Hook事件
- PAT B1009 說反話(簡單模擬)
- 【Tomcat】Tomcat工作原理及簡單模擬實現Tomcat
- H3C模擬器HCL簡單配置OSPF
- 利用Easy Mock簡單模擬開發資料介面Mock
- PAT-B 1012 數字分類【簡單模擬】
- 使用 Raku 編寫簡單的文字識別模擬程式
- Proteus實現簡單51程式的設計與模擬
- 獎金up up up!單個漏洞最高獎勵2萬元!
- Flink Sort-Shuffle 實現簡介
- noip模擬29[簡單的板子題](雖然我不會)
- 使用 json-server 簡單完成CRUD模擬後臺資料JSONServer
- Mac物理數值模擬模擬軟體Comsol Multiphysics中文破解版 附安裝教程 相容M1/M2Mac
- 簡單聊聊模組
- 透過簡單實現 PHP 請求模擬賺了 50 元PHP
- 簡要MR與Spark在Shuffle區別Spark
- 模擬實現簡易版shell
- PAT-B 1008 陣列元素迴圈右移問題【簡單模擬】陣列
- Paramiko模組簡單使用
- C# Post 模擬表單提交C#
- 模擬select下拉選單詳解
- 單元測試模擬框架Mockito框架Mockito
- 陣列模擬單連結串列陣列
- 回顧二分與bfs(或者說是遞推)和簡單模擬
- python inspect模組簡單使用Python
- 支援M1 模擬模擬軟體Tecplot2021r1 Mac破解下載 附完整安裝教程Mac
- 1284 海港 普及組 NOIP2016 佇列基礎 簡單列舉 簡單模擬 優先佇列(priority_queue)佇列
- M31系列LoRa分散式IO模組功能簡介分散式
- Ryujinx for Mac(任天堂Switch模擬器)支援M晶片圖文安裝教程Mac晶片
- M – Sort it 一組簡單一點的題目(三)
- 簡單介紹python process模組Python
- 前端模組化簡單總結前端