双向队列
TimeLimit: 1000MS Memory Limit: 65536KB
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: Accepted
Take time: 4ms
Take Memory: 200KB
Submit time: 2017-01-13 21:12:59
****************************************************/