双向队列
Time Limit: 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<iostream>#include<algorithm>
#include<deque>
#include<queue>
#include<string.h>
using namespace std;
int main()
{
int n;
int x;
deque<int>s;
queue<int>w;
cin>>n;
char a[5];
int m;
m=n;
while(n--)
{
cin>>a;
if(strcmp(a,"LIN")==0)
{
cin>>x;
s.push_back(x);
}
else if(strcmp(a,"RIN")==0)
{
cin>>x;
s.push_front(x);
}
else if(strcmp(a,"ROUT")==0)
{
if(!s.empty())
s.pop_front();
else
w.push(m-n);
}
else if(strcmp(a,"LOUT")==0)
{
if(!s.empty())
s.pop_back();
else
w.push(m-n);
}
}
if(s.empty())
{
while(!w.empty())
{
cout<<w.front()<<" "<<"ERROR"<<endl;
w.pop();
}
}
else
{
while(!s.empty())
{
cout<<s.back()<<" ";
s.pop_back();
}
cout<<endl;
while(!w.empty())
{
cout<<w.front()<<" "<<"ERROR"<<endl;
w.pop();
}
}
}