AcWing 3160. 拉马车
原题链接
简单
作者:
卐_8
,
2024-04-04 10:04:32
,
所有人可见
,
阅读 4
#include<map>
#include<vector>
#include<string>
#include<queue>
#include<iostream>
using namespace std;
queue<char> A;//存两个人的牌
queue<char> B;
vector<char> v;//存桌上的牌
string a, b;
map<char, int> m;//记录某张牌在哪个位置
int t;
int main()
{
cin >> a >> b;
for (int i = 0; i < a.size(); i++)
A.push(a[i]);
for (int i = 0; i < b.size(); i++)
B.push(b[i]);
int count = 0;
bool flag = true; //用于赢牌后继续出牌
while (!A.empty() and !B.empty())
{
if (t >= 100000)
{
cout << -1 << endl;
return 0;
}
t++;
if (flag == true)
{
//a出牌
v.push_back(A.front());
//桌子上有a的牌
if (m.count(A.front()))
{
int index = m[A.front()];
for (int i = count; i >= index; i--)
{
A.push(v[i]);
m.erase(v[i]);
v.pop_back();
count--;
}
count++;
A.pop();
continue;
}
//桌子上没有A的牌
m[A.front()] = count;
count++;
A.pop();
}
if (A.empty() || B.empty())
{
break;
}
//b出牌
v.push_back(B.front());
//桌子上有b的牌
if (m.count(B.front()))
{
int index = m[B.front()];
for (int i = count; i >= index; i--)
{
B.push(v[i]);
m.erase(v[i]);
v.pop_back();
count--;
}
B.pop();
count++;
flag = false;
continue;
}
//桌子上没有B的牌
flag = true;
m[B.front()] = count;
count++;
B.pop();
}
//A为空
if (A.empty() == true)
{
int k = B.size();
for (int i = 0; i < k; i++)
{
cout << B.front();
B.pop();
}
}
//B为空
else // (B.empty()==true)
{
int k = A.size();
for (int i = 0; i < k; i++)
{
cout << A.front();
A.pop();
}
}
return 0;
}