题意:
有一堆书,两种操作。
ADD:往顶部加入一本书
ROTATE:翻转书堆顶部的前k本书
思路:
deque(双向队列)
利用两个双向队列,q1储存前k个,q2储存超过k个的剩余部分。
设置flag变量表示对待q1应该往前插入或是往后插入,q2都是往前插入。
最后输出的时候还得看flag变量。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
deque<string> q1,q2;
int n,m,k;
string x;
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<min(n,k); i++)
{
cin >> x;
q1.push_back(x);
}
for(int i=0; i<n-k; i++)
{
cin >> x;
q2.push_back(x);
}
int flag=1;
while(m--)
{
cin >> x;
if(x[0]=='A')
{
int id=x.find(")");
string z=x.substr(4,id-4);
if(flag==1)
q1.push_front(z);
else if(flag==-1)
q1.push_back(z);
if(q1.size()>k)
{
if(flag==1)
{
q2.push_front(q1.back());
q1.pop_back();
}
else if(flag==-1)
{
q2.push_front(q1.front());
q1.pop_front();
}
}
}
else
{
flag=-flag;
}
}
if(flag==1)
{
while(!q1.empty())
{
cout << q1.front() << endl;
q1.pop_front();
}
}
else if(flag==-1)
{
while(!q1.empty())
{
cout << q1.back() << endl;
q1.pop_back();
}
}
while(!q2.empty())
{
cout << q2.front() << endl;
q2.pop_front();
}
return 0;
}