SGU 271Book Pile(模拟 deque+stack)

271. Book Pile

time limit per test: 0.25 sec.
memory limit per test: 65536 KB
input: standard
output: standard



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
 
  






题目大意:

桌子上有一摞书n本,对于这摞书有两种操作:

1、添加一本书到最上面  ADD(书名s)

2、使最上面k本书逆序,如果总数小于k则所有书逆序  ROTATE


使用双端队列存放最上面的<=k本书,如果书本多余了k本,就把多余的保存下来放到一个结果数组或者栈里面。如果rotate一次,双端的进入出口方向发生互换即可。


   题目地址:271. Book Pile


AC代码:

#include
#include
#include
#include
#include
#include
using namespace std;

char a[105];
char tmp[105];

int main()
{
    int n,m,k,i;
    int flag;
    while(cin>>n>>m>>k)
    {
        deque mq;
        stack ans;
        flag=0;
        while(n--)
        {
            scanf("%s",a);
            mq.push_back(a);
        }

        while(mq.size()>k)
        {
            ans.push(mq.back());
            mq.pop_back();
        }

        while(m--)
        {
            scanf("%s",a);
            if(a[0]=='R')
            {
                 flag=1-flag;    //翻转,相当于双端队列出入变换
                 continue;
            }
            int t=0;
            for(i=4;ik)
                {
                    ans.push(mq.back());
                    mq.pop_back();
                }
            }
            else
            {
                mq.push_back(tmp);
                if(mq.size()>k)
                {
                    ans.push(mq.front());
                    mq.pop_front();
                }
            }
        }

        if(!flag)
        {
            while(!mq.empty())
            {
                ans.push(mq.back());
                mq.pop_back();
            }
        }
        else
        {
            while(!mq.empty())
            {
                ans.push(mq.front());
                mq.pop_front();
            }
        }

        while(!ans.empty())
        {
            cout<



你可能感兴趣的:(STL)