【题目描述】
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.
扑克玩家在扑克桌上的一种常见的消遣方式是洗牌一堆筹码。洗牌筹码是从两堆扑克筹码开始执行,S1和S2,每个堆栈包含C芯片。每个堆栈可能包含几种不同颜色的芯片。
The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:
实际的洗牌操作是通过将S1芯片与S2芯片交织来执行的,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.
结果堆栈S12包含2*C芯片。S12的最底层芯片是S2中最底层的芯片。在该芯片的顶部,是S1中最底层的芯片。交织过程继续从S2的底部取出第2芯片,并将其放置在S12上,然后从S1的底部取第2芯片等等,直到S1的最顶层芯片被放置在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.
在洗牌操作之后,S12被分成两个新的堆栈,从S12取最底层的C芯片形成一个新的S1,而从S12的最顶端的C芯片形成一个新的S2。然后,可以重复洗牌操作以形成新的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.
对于这个问题,您将编写一个程序来确定一个特定的结果堆栈S12是否可以通过多次调整两个堆栈来形成。
【输入】
The first line of input contains a single integer N,(1 ≤ N ≤ 1000) which is the number of datasets that follow.
第一行输出一个整数N(1 ≤ N ≤ 1000),N是后面的数据集个数。
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.
每个数据集包括4行输入,数据集的第一行是一个整数C(1 ≤ C ≤ 100),C是每个初始堆栈(S1和S2)中的芯片数。每个数据集的第二行指定堆栈S1中每个C芯片的颜色,从最底层的芯片开始。每个数据集的第三行指定堆栈S2中每个C芯片的颜色,从最底层的芯片开始。颜色用一个大写字母(A到H)表示。芯片颜色之间没有空白或分隔符。每个数据集的第四行包含2*C大写字母(A到H),表示S1和S2 0次或多次洗牌所需结果的颜色。首先指定最底层芯片的颜色。
【输出】
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.
每个数据集的输出由一行组成,该行显示数据集编号(1到N),一个空格和一个整数值,它是获得所需结果堆栈所需的最少洗牌操作数。如果无法使用数据集的输入来达到所需的结果,则为洗牌操作的次数显示值负1(−1)。
【样例输入】
2
4
AHAH
HAHA
HHAAAAHH
3
CDE
CDE
EEDDCC
【样例输出】
1 2
2 -1
题目链接:https://cn.vjudge.net/problem/POJ-3087
模拟操作即可,用set记录操作过程中出现的所有S12,如果出现重复则说明不可能。
代码如下:
#include <iostream>
#include <set>
#include <queue>
#include <string>
using namespace std;
int l;
string s1,s2,s;
struct Node{
string s;
int step;
};
void bfs()
{
set<string> S;
queue<Node> Q;
Node no;
for(int i=0;i<l;i++)
{
no.s+=s2[i];
no.s+=s1[i];
}
no.step=1;
Q.push(no);
S.insert(no.s);
while(!Q.empty())
{
Node now=Q.front();
Q.pop();
if(now.s==s)
{
cout<<now.step<<endl;
return;
}
string st1,st2;
for(int i=0;i<l;i++)
st1+=now.s[i];
for(int i=l;i<2*l;i++)
st2+=now.s[i];
Node next;
for(int i=0;i<l;i++)
{
next.s+=st2[i];
next.s+=st1[i];
}
if(!S.count(next.s))
{
next.step=now.step+1;
Q.push(next);
}
else
{
cout<<-1<<endl;
return;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
int T;
cin>>T;
for(int kase=1;kase<=T;kase++)
{
cin>>l;
cin>>s1>>s2>>s;
cout<<kase<<" ";
bfs();
}
return 0;
}