sdutacm-双向队列

来源:互联网 发布:网络访问控制软件 编辑:程序博客网 时间:2024/06/06 10:55

双向队列

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

Problem Description

     想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。
现在给你一系列的操作,请输出最后队列的状态;
命令格式:
LIN X  X
表示一个整数,命令代表左边进队操作;
RIN X  
表示右边进队操作;
ROUT
LOUT   
表示出队操作;

Input

第一行包含一个整数M(M<=10000),表示有M个操作;
以下M行每行包含一条命令;
命令可能不合法,对于不合法的命令,请在输出中处理;

Output

输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;
以下若干行处理不合法的命令(如果存在);
对于不合法的命令,请输出一行X ERROR
其中X表示是第几条命令;

Example Input

8

LIN 5

RIN 6

LIN 3

LOUT

ROUT

ROUT

ROUT

LIN 3

Example Output

3

7 ERROR

Hint

 

Author

wanglin

#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#include<algorithm>#include<queue>#include<deque>#include <iostream>using namespace std;int main(){   //freopen("ass","w",stdout);    int m,j=0;    char g[10];    int pp[11002];    deque<int> p;    scanf("%d",&m);    for(int i=1;i<=m;i++)    {      scanf("%s",g);      if(strcmp(g,"LIN")==0)      {          int k;          scanf("%d",&k);          p.push_front(k);      }      else if(strcmp(g,"RIN")==0)      {           int k;           scanf("%d",&k);           p.push_back(k);      }      else if(strcmp(g,"ROUT")==0)      {                if(p.empty())                {                 pp[j++] = i;                }                else p.pop_back();      }      else if(strcmp(g,"LOUT")==0)      {         if(p.empty())         {            pp[j++] = i;         }         else        {        p.pop_front();         }      }      }      int top =1;      while(!p.empty())      {         if(top) top = 0;         else printf(" ");         printf("%d",p.front());         p.pop_front();      }      printf("\n");for(int e =0;e<j;e++)      {        printf("%d ERROR\n",pp[e]);      }    return 0;}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 4msTake Memory: 200KBSubmit time: 2017-01-13 21:12:59****************************************************/

0 0
原创粉丝点击