双向队列

来源:互联网 发布:通用域名注册 编辑:程序博客网 时间:2024/05/01 11:49

双向队列

TimeLimit: 1000ms Memory limit: 65536K有疑问?点这里^_^

题目描述

想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首;两头都可以做出队,入队的操作。

现在给你一系列的操作,请输出最后队列的状态;

命令格式:

LINX X表示一个整数,命令代表左边进队操作;

RINX表示右边进队操作;

ROUT

LOUT表示出队操作;

输入

第一行包含一个整数M(M<=10000),表示有M个操作;

以下M行每行包含一条命令;

命令可能不合法,对于不合法的命令,请在输出中处理;

输出

输出的第一行包含队列进行了M次操作后的状态,从左往右输出,每两个之间用空格隔开;

以下若干行处理不合法的命令(如果存在);

对于不合法的命令,请输出一行XERROR

其中X表示是第几条命令;

示例输入

8

LIN5

RIN6

LIN3

LOUT

ROUT

ROUT

ROUT

LIN3

示例输出

3

7ERROR

#include <map>#include <set>#include <cmath>#include <stack>#include <queue>#include <time.h>#include <cstdio>#include <cctype>#include <vector>#include <string>#include <climits>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define RR freopen("input.txt","r",stdin)#define WW freopen("output.txt","w",stdout)#define INF 0x3f3f3f3fusing namespace std;const int Max=30000;int a[Max];int b[10000];int L,R,top;int main(){    int n,data;    char s[10];    top=0;    L=Max/2;    R=Max/2;    scanf("%d",&n);    for(int i=1; i<=n; i++)    {        scanf("%s",s);        if(strcmp(s,"LIN")==0)        {            scanf("%d",&data);            a[--L]=data;        }        else if(strcmp(s,"RIN")==0)        {            scanf("%d",&data);            a[R++]=data;        }        else if(strcmp(s,"LOUT")==0)        {            if(L<R)//<span style="font-family:华文楷体, serif;">判断是否合法</span>            {                L++;            }            else            {                b[top++]=i;            }        }        else if(strcmp(s,"ROUT")==0)        {            if(L<R)            {                R--;            }            else            {                b[top++]=i;            }        }    }    if(L<R)    {        for(int i=L; i<R; i++)        {            if(i!=L)                cout<<" ";            cout<<a[i];        }        cout<<endl;    }    for(int i=0; i<top; i++)    {        cout<<b[i]<<" "<<"ERROR"<<endl;    }    return 0;}


0 0