#include<iostream>
#include<queue>
using namespace std;
//2017 蓝桥c组c语言第九题拉马车
/*典型的队列和栈的应用
用到STL里面的队列
因为STL里面的栈没有遍历的功能自己模拟了一个
其实思路很清晰 把这个过程模拟出来就好了
有些地方可能有点冗余和bug欢迎指出
*/
char path[1000]; // 一个辅助栈
int top=-1; // 栈顶指针
queue<char> A;
queue<char> B;
int locate(char ch){ //在栈里寻找的下标 找不到就返回-1
for(int i=top;i>=0;i--)
{
if(path[i]==ch)
return i;
}
return -1;
}
void push(char ch){ // 模拟压栈
path[++top]=ch;
}
char pop(){ // 模拟出栈
return path[top--];
}
void init(){
char a[]="96j5a898qa"; //测试数据 (A的手牌)
char b[]="6278a7q973"; //测试数据 (B的手牌)
for(char* temp=a;(*temp)!='\0';temp++) //进队
A.push(*temp);
for(char* temp=b;(*temp)!='\0';temp++) //进队
B.push(*temp);
}
int main(){
init();
char a; //a当前出牌
char b; //b当前出牌
int count=0;
int x1=1; //a当前的优先级
int x2=-1; //b当前的优先级
while(1){
if(x1>x2){ //A先出牌
a=A.front(); //获取当前牌
A.pop(); //从队列去除当前牌
int x=locate(a); //查看当前栈里该元素的位置 没有则返回-1
push(a);
//压栈
if(x!=-1){
for(;top>=x;) //出栈到指定位置
A.push(pop());
x1=1; //改变优先级
x2=-1;
continue;
}
else{
if(A.empty()) //如果当前手牌为空结束游戏
break;
}
b=B.front();
B.pop();
int y=locate(b);
push(b);
if(y!=-1){
for(;top>=y;)
B.push(pop());
x1=-1;
x2=1;
continue;
}
else{
if(B.empty())
break;
}
}
else { //B先出
b=B.front();
int y=locate(b);
B.pop();
path[++top]=b;
if(y!=-1){
for(;top>=y;top--)
B.push(path[top]);
x1=-1;
x2=1;
continue;
}
else{
if(B.empty())
break;
}
a=A.front();
A.pop();
int x=locate(a);
push(a);
if(x!=-1){
for(;top>=x;)
A.push(pop());
x1=1;
x2=-1;
continue;
}
else{
if(A.empty())
break;
}
}
}
if(A.empty()&&B.empty()){
cout<<-1;
}
else if(!A.empty()){
while(!A.empty()){
cout<<A.front();
A.pop();
}
}
else {
while(!B.empty()){
cout<<B.front();
B.pop();
}
}
}