There is a pile of N books on the table. Two types of operations are performed over this pile:
- a book is added to the top of the pile,
- top K books are rotated. If there are less than K books on the table, the whole pile is rotated.
First operation is denoted as ADD(S) where S is the name of the book, and the second operations is denoted as ROTATE.
The maximum number of books is no more than 40000. All book names are non-empty sequences of no more than 3 capital Latin letters. The names of the books can be non-unique.
Input
The first line of input file contains 3 integer numbers N, M, K (0 <= N <= 40000; 0 <= M <= 100000; 0 <= K <= 40000). The following N lines are the names of the books in the pile before performing any operations. The book names are given in order from top book to bottom. Each of the following M lines contains the operation description.
Output
Output the sequence of books names in the pile after performing all operations. First line corresponds to the top book.
Sample test(s)
Input
2 3 2
A
B
ADD(C)
ROTATE
ADD(D)
Output
D
A
C
B
按照题目要求大模拟就一定会超时,由于只动了前面的字符,所以可以选择双端队列。
*注意,字符可能有多个。
#include <iostream>
#include <cstring>
#include <deque>
#include <cstdlib>
#include <cstdio>
#define INF 0x3f3f3f3f
using namespace std;
deque<string>ans1, ans2;
int main()
{
int n, m, k;
ios::sync_with_stdio(false);
cin>>n>>m>>k;
for(int i=0; i<n; i++)
{
string x;
cin>>x;
if(i<k)
ans1.push_back(x);
else
ans2.push_back(x);//hou
}
int flag = 0;
while(m--)
{
string c;
cin>>c;
if(c[0]=='A')
{
string ka=c.substr(4, c.size()-5);//注意这里,名字可能不是一个字符
if(flag==0)
{
ans1.push_front(ka);
if(ans1.size()>k)
{
ans2.push_front(ans1.back());
ans1.pop_back();
}
}
else
{
ans1.push_back(ka);
if(ans1.size()>k)
{
ans2.push_front(ans1.front());
ans1.pop_front();
}
}
}
else
{
if(!flag)
flag = 1;
else
flag = 0;
}
}
if(flag==0)
{
while(!ans1.empty())
{
cout<<ans1.front()<<endl;
ans1.pop_front();
}
}
else
{
while(!ans1.empty())
{
cout<<ans1.back()<<endl;
ans1.pop_back();
}
}
while(!ans2.empty())
{
cout<<ans2.front()<<endl;
ans2.pop_front();
}
return 0;
}