Game 1000(ms) 10000(kb) 21 / 72One day, Caspar’s girlfriend Rapsac asked Caspar to play a game with her. This game called “The Trains”. There are some cards, each of them is marked with an integer K (1<=K<=200). Before the beginning of the game, they divide them to 2 sequences, one for Caspar and another for Rapsac. Then, they pick the first card of their cards out and put it to the game sequence in turn. The rule is that if one picks out card P and there has been already a card P in the game sequence, he or she would take the cards from the first card P to the end of the game sequence and put them to end of his or her sequence. And if someone has no card in his or her turn, he or she will lose. 输入
There are multiple testcases.
The first line of each testcase contains two integer N (1<=N<= 200) and M (1<=M<=200). The second line has N integers, indicating the sequence of cards of Caspar. The next line has M integers, indicating the sequence of cards of Rapsac. Caspar is always the first one to pick up card.
输出For each testcase, output the winner’s name.样例输入3 4
1 3 1
2 3 2 1
样例输出
Caspar
提 交
g++1 1
翻译过来的意思就是两个轮流在桌面上叠加放牌,有相同的就将相同的牌及中间夹的牌收回来,注意,要按照原排序顺序收回来。
所以在代码中用了两个栈,一个栈来倒了一下顺序。
下面贴上代码啦,有问题评论区里留哦。
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b){
int v[1005]={0};
queue<int>q1,q2;
stack<int>s,s1;
for(int i=0;i<a;i++){
int d;
cin>>d;
q1.push(d);
}
for(int i=0;i<b;i++){
int d;
cin>>d;
q2.push(d);
}
int top=1,flag=0,mark=0;
while(!q1.empty()&&!q2.empty()){
if(top%2!=0){
int d=q1.front();
if(v[d]==1){
v[d]=0;
if(flag==0)
mark=1;
q1.pop();
q1.push(d);
while(s.top()!=d){
int d4=s.top();
v[d4]=0;
s1.push(d4);
s.pop();
}
while(!s1.empty()){
int d6=s1.top();
q1.push(d6);
s1.pop();
}
q1.push(d);
s.pop();
}
else{
v[d]=1;
s.push(d);
q1.pop();
}
}
else{
int d2=q2.front();
if(v[d2]==1){
v[d2]=0;
if(flag==0)
mark=2;
q2.pop();
q2.push(d2);
while(s.top()!=d2){
int d3=s.top();
v[d3]=0;
s1.push(d3);
s.pop();
}
while(!s1.empty()){
int d5=s1.top();
q2.push(d5);
s1.pop();
}
q2.push(d2);
s.pop();
}
else{
v[d2]=1;
s.push(d2);
q2.pop();
}
}
top++;
if(mark!=0)
flag=1;
}
if(q1.empty())
cout<<"Rapsac"<<endl;
else
cout<<"Caspar"<<endl;
}
}