copy的山青.
题目描述:
某天,空和白感到无聊便开始打牌,输入两个字符串 a,b 表示空和白的纸牌队列,空先出,牌的组成为[‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘X’,‘J’,‘Q’,‘K’,‘A’,‘2’,’#’],出牌的顺序沿着字符串从左到右,两方轮流出牌放在桌面上纸牌队列的末尾。
当出的牌与桌面上的某张牌相同时,可以获得这两张牌之间的所有的牌,并按从后往前的顺序收取纸牌,放在自己纸牌队列的末尾。赢牌的一方继续出牌,直到桌面上没有与出的牌相同时轮到对方出牌。’#’ 是王,它拥有特殊的能力,当出 ‘#’ 时,可以直接获得桌面上所有的牌。当一方没牌可出的时候,比赛结束,输出获胜方的纸牌。若游戏无法结束,输出 -1。
例如: a 为 “32#A” ,b 为 “A32” ,空出 ‘3’,白出 ‘A’,空出 ‘2’,白出 ‘3’,此时桌面上的牌为 “3A23” ,存在与出的牌相同的牌,白可以获得 “3A23”,从后往前收取,放在自己纸牌队列的末尾,则白的牌为 “232A3”,因为白赢了牌,所以她需要继续出牌,白出 ‘2’,空出 ‘#’,空可以收取桌面上所有的牌,空的牌变为 “A#2”。
输入描述:
第一行输入一个 T(T≤50)T (T\le50)T(T≤50) ,表示测试样例数。
接下来的 2∗T2*T2∗T 行,每两行表示一个测试样例。
每个字符串的长度不超过505050。
输出描述:
输出获胜方的纸牌,若游戏无法结束,输出 -1。
样例输入
2
96J5A89866
8A7Q973
582X5J2A2AAJ#5744
69K#4A8Q846447X
样例输出
Q57JA86899676
-1
题解思路
用两个队列维护空和白的手牌,用一个栈维护牌堆,用一个set表示在场上的牌,按照题意模拟即可。对于无解的情况,只需要加一个计数器,对游戏回合进行计数,当计数器足够大的时候即可判定为无解。
比赛的时候跟队友12道题只写出来了6道混了个二等奖,这一道题最后一直在写但可能是心态有点崩没能写出来,现在静下心来写发现并不难,因为时间限制为三秒,所以感觉不用怎么担心超时的问题。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<cstring>
#define ll long long
#define IO std::ios::sync_with_stdio(false);
using namespace std;
const int maxn=1e5+10;
int main()
{
IO;
int n;
cin>>n;
while(n--)
{
string stra,strb;
cin>>stra>>strb;
queue<char> a,b;
stack<char> st;
set<char> s;
for(int i=0;i<stra.size();i++)
{
a.push(stra[i]);
}
for(int i=0;i<strb.size();i++)
{
b.push(strb[i]);
}
ll sum=0;
bool f=true;
while(f)
{
if(b.empty())
{
while(!a.empty())
{
cout<<a.front();
a.pop();
}
cout<<endl;
break;
}
while(f)
{
st.push(a.front());
a.pop();
sum++;
if(st.top()=='#')
{
s.clear();
while(!st.empty())
{
a.push(st.top());
st.pop();
}
if(sum>=maxn)
{
f=false;
break;
}
continue;
}
if(s.count(st.top()))
{
char temp=st.top();
a.push(temp);
st.pop();
while(st.top()!=temp)
{
char t=st.top();
s.erase(t);
a.push(t);
st.pop();
}
s.erase(temp);
a.push(temp);
st.pop();
if(sum>=maxn)
{
f=false;
break;
}
continue;
}
else
{
s.insert(st.top());
break;
}
}
if(sum>=maxn)
{
f=false;
break;
}
if(a.empty())
{
while(!b.empty())
{
cout<<b.front();
b.pop();
}
cout<<endl;
break;
}
while(f)
{
st.push(b.front());
b.pop();
sum++;
if(st.top()=='#')
{
s.clear();
while(!st.empty())
{
b.push(st.top());
st.pop();
}
if(sum>=maxn)
{
f=false;
break;
}
continue;
}
if(s.find(st.top())!=s.end())
{
char temp=st.top();
b.push(temp);
st.pop();
while(st.top()!=temp)
{
char t=st.top();
s.erase(t);
b.push(t);
st.pop();
}
s.erase(temp);
b.push(temp);
st.pop();
if(sum>=maxn)
{
f=false;
break;
}
continue;
}
else
{
s.insert(st.top());
break;
}
}
if(sum>=maxn)
{
f=false;
break;
}
}
if(!f)
{
cout<<-1<<endl;
}
}
return 0;
}