Shuffle'm Up——簡單模擬

【題目描述】

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

【題目分析】
感覺這道題并不是經典的搜索,完全是一個模擬,也沒有多種情況,在網上看其他人的博客好像都是用的BFS或者是DFS,覺得不必要
關鍵還是判斷無法洗出那樣情況的判斷,發現用set很方便,將所有出現過的情況都加入到set里面,一旦重復就說明洗不出來了(這個時候還沒有出現想要洗出的牌,那么繼續洗下去只會一直重復循環)【AC代碼】

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<climits>
#include<set>using namespace std;int n,ans;
string s1,s2,s12,s;string cat(const string& s1,const string& s2)
{string cnt="";for(int i=0;i<n;i++){cnt.push_back(s2[i]);cnt.push_back(s1[i]);}return cnt;
}bool work()
{string a,b;set<string> st;ans=1;s=cat(s1,s2);st.insert(s);while(1){//cout<<s<<" "<<s12<<endl;if(s==s12){return true;}a=s.substr(0,n);b=s.substr(n,2*n);s=cat(a,b);ans++;if(st.find(s)!=st.end())return false;st.insert(s);}
}int main()
{//ios::sync_with_stdio(false);//發現vjudge上用不了這個優化,不知道為什么int T;scanf("%d",&T);for(int V=1;V<=T;V++){scanf("%d",&n);cin>>s1>>s2>>s12;if(work()){printf("%d %d\n",V,ans);}else{printf("%d -1\n",V);}}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384153.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384153.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384153.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++ explicit關鍵字詳解

http://www.cnblogs.com/ymy124/p/3632634.html 首先, C中的explicit關鍵字只能用于修飾只有一個參數的類構造函數, 它的作用是表明該構造函數是顯示的, 而非隱式的, 跟它相對應的另一個關鍵字是implicit, 意思是隱藏的,類構造函數默認情況下即聲明為implicit(隱式). 那么顯示聲…

Fire!——兩個BFS

【題目描述】 【題目分析】 看到題目后很清楚是兩個BFS&#xff0c;可是我覺得對于火的BFS可以轉換成判斷&#xff0c;我的做法是將火的位置全部記錄下來&#xff0c;然后判斷某個位置距離每個火的步數是否小于當前步數&#xff0c;可是錯了&#xff0c;還不清楚為什么&#x…

函數調用過程(棧楨)

棧楨 首先來看一段代碼 #include<stdio.h> int add(int x, int y) {int z x y;return z; } int main() {int a 10;int b 20;int ret add(a, b);printf("ret %d\n",ret);return 0; } 此處是為了給a,b分別開辟空間,這時棧楨如圖所示 兩條push命令將a,b變…

C++智能指針簡單剖析

http://blog.csdn.net/lanxuezaipiao/article/details/41603883 導讀 最近在補看《C Primer Plus》第六版&#xff0c;這的確是本好書&#xff0c;其中關于智能指針的章節解析的非常清晰&#xff0c;一解我以前的多處困惑。C面試過程中&#xff0c;很多面試官都喜歡問智能指針相…

非常可樂——BFS

【題目描述】 大家一定覺的運動以后喝可樂是一件很愜意的事情&#xff0c;但是seeyou卻不這么認為。因為每次當seeyou買了可樂以后&#xff0c;阿牛就要求和seeyou一起分享這一瓶可樂&#xff0c;而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子&#xff0c;它們的容…

整型數據存儲

//代碼1 #include<stdio.h> int main() {char a -1;signed char b -1;unsigned char c -1;printf("a %d, b %d, c %d", a, b, c);return 0; } 1000 0000 0000 0001 -> -1源碼 1111 1111 1111 1110 -> -1反碼 1111 1111 1111 1111 -> -1補碼 對于…

聊聊gcc參數中的-I, -L和-l

http://blog.csdn.net/stpeace/article/details/49408665 在本文中&#xff0c; 我們來聊聊gcc中三個常見的參數&#xff0c; 也即-I, -L和-l 一. 先說 -I (注意是大寫的i) 我們先來看簡單的程序&#xff1a; main.c: [cpp] view plaincopy #include <stdio.h> #incl…

Pots——BFS

【題目描述】 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j;…

HDU - 4578Transformation——線段樹+區間加法修改+區間乘法修改+區間置數+區間和查詢+區間平方和查詢+區間立方和查詢

【題目描述】 HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between ax …

[C++基礎]034_C++模板編程里的主版本模板類、全特化、偏特化(C++ Type Traits)

http://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/18/2728753.html 1. 主版本模板類 首先我們來看一段初學者都能看懂&#xff0c;應用了模板的程序&#xff1a; 1 #include <iostream>2 using namespace std;3 4 template<class T1, class T2>5 clas…

自定義類型: 結構體,枚舉,聯合

1.結構體 個人認為結構體和數組特別相似&#xff0c;只不過結構體和數組的區別在于結構體的成員可以是不同類型&#xff0c;而數組成員類型是相同的。 &#xff08;1&#xff09;結構體的聲明 struct tag {成員列表//至少得有一個成員 }值列表;//值列表可以省略 struct {int a…

詳解C++中的函數調用和下標以及成員訪問運算符的重載

http://www.jb51.net/article/78436.htm 這篇文章主要介紹了詳解C中的函數調用和下標以及成員訪問運算符,講到了這些二元運算符使用的語法及重載,需要的朋友可以參考下函數調用 使用括號調用的函數調用運算符是二元運算符。 語法 ?1primary-expression ( expression-list )備…

A計劃——BFS

【題目描述】 可憐的公主在一次次被魔王擄走一次次被騎士們救回來之后&#xff0c;而今&#xff0c;不幸的她再一次面臨生命的考驗。魔王已經發出消息說將在T時刻吃掉公主&#xff0c;因為他聽信謠言說吃公主的肉也能長生不老。年邁的國王正是心急如焚&#xff0c;告招天下勇士…

使用openssl的md5庫

http://blog.csdn.net/sinat_35297665/article/details/78244523 在linux機器上&#xff0c;有一個命令可以計算出文件的md5值&#xff0c;那就是md5sum&#xff0c;如果沒有的話&#xff0c;就需要安裝RPM包&#xff1a;coreutils。 現在我們使用openssl的庫也可以方便的計算出…

主席樹入門

今天學習了一下主席樹&#xff08;名字這么強的嘛&#xff09; 雖然直接理解起來不容易&#xff0c;但是這種解決問題的思想其實并不陌生。 我們可以首先來看維護整個區間第K大的線段樹 我們將[l,r]區間內數字的多少用線段樹進行維護&#xff0c;這樣的話為了求取區間第k大&…

Socket網絡編程--小小網盤程序(1)

http://www.cnblogs.com/wunaozai/p/3886588.html 這個系列是準備講基于Linux Socket進行文件傳輸。簡單的文件傳輸就是客戶端可以上傳文件&#xff0c;可以從服務器端下載文件。就這么兩個功能如果再加上身份驗證&#xff0c;就成了FTP服務器了&#xff0c;如果對用戶的操作再…

使用 Verdaccio 構建自己的私有 npm 倉庫

前言 無論你是公司的開發者&#xff0c;還是個人開發者&#xff0c;你可能都聽說過或者使用過 npm&#xff0c;這是一個使用廣泛的 JavaScript 包管理器。但是&#xff0c;你是否遇到過以下的問題&#xff1a;你需要一個私有的包存放地方&#xff0c;或者你需要在離線環境下使…

HDU - 4348To the moon——主席樹+區間修改

HDU - 4348To the moon 【題目描述】 【題目分析】 題目中說明每次更新后時間都會加1&#xff0c;而且還會需要查詢以前的區間&#xff0c;還會需要返回以前的時間&#xff0c;所以是很裸的主席樹。區間查詢的話我們同樣需要用到lazy標記 通過這道題我發現線段樹的操作還是很靈…

進入一個目錄需要那些權限

1.文件訪問者的分類 文件的訪問者具體可分為以下幾類&#xff1a; (1)擁有者 (2)組擁有者 (3)其他用戶 這些都代表什么意思呢&#xff1f; 其中r表示只讀&#xff0c;w表示只寫&#xff0c;x表示可執行&#xff0c;第一個字母代表了文件的類型&#xff0c;其中文件類型可以分為…

Socket網絡編程--小小網盤程序(2)

http://www.cnblogs.com/wunaozai/p/3887728.html 這一節將不會介紹太多的技術的問題&#xff0c;這節主要是搭建一個小小的框架&#xff0c;為了方便接下來的繼續編寫擴展程序。本次會在上一小節的基礎上加上一個身份驗證的功能。 因為網盤程序不像聊天程序&#xff0c;網盤是…