light oj 1212 - Double Ended Queue (双向队列)
2016-09-16 16:33
671 查看
1212 - Double Ended Queue
A queue is a data structure based on the principle of 'First In First Out' (FIFO). There are two ends; one end can be used only to insert an item and the other end to remove an item. A Double Ended Queue is a queue where you can insert an item in both sides
as well as you can delete an item from either side. There are mainly four operations available to a double ended queue. They are:
1. pushLeft(): inserts an item to the left end of the queue with the exception that the queue is not full.
2. pushRight(): inserts an item to the right end of the queue with the exception that the queue is not full.
3. popLeft(): removes an item from the left end of the queue with the exception that the queue is not empty.
4. popRight(): removes an item from the right end of the queue with the exception that the queue is not empty.
Now you are given a queue and a list of commands, you have to report the behavior of the queue.
Each case starts with a line containing two integers n, m (1 ≤ n ≤ 10, 1 ≤ m ≤ 100), where n denotes the size of the queue and m denotes the number of commands. Each of the next m lines
contains a command which is one of:
pushLeft x pushes x (-100 ≤ x ≤ 100) in the left end of the queue
pushRight x pushes x (-100 ≤ x ≤ 100) in the right end of the queue
popLeft pops an item from the left end of the queue
popRight pops an item from the right end of the queue
双向队列,如上图,左右两端都可以既队列又出队列;
解:这题的数据不大,开一个数组,从中间开始模拟队列的进出
代码:
/*
l, r 是加是减 用具体几个数试试就清楚了
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int t,k=1,n,m,x,l,r;
int a[30]; //队列已经限制小于10
char str[20];
cin>>t;
while(t--)
{
cout<<"Case "<<k++<<":"<<endl;
int l=r=15;//从中间开始
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>str;
if(str[1]=='u')//进队列的情况
{
cin>>x;
if(r-l>=n)
cout<<"The queue is full"<<endl;
else
{
if(str[4]=='L')
{
cout<<"Pushed in left: "<<x<<endl;
a[l]=x;l--;
}
else
{
cout<<"Pushed in right: "<<x<<endl;
r++;a[r]=x;
}
}
}
else//出队列的情况
{
if(l==r)
cout<<"The queue is empty"<<endl;
else
{
if(str[3]=='L')
{
cout<<"Popped from left: "<<a[l+1]<<endl;
l++;
}
else
{
cout<<"Popped from right: "<<a[r]<<endl;
r--;
}
}
}
}
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 0.5 second(s) | Memory Limit: 32 MB |
as well as you can delete an item from either side. There are mainly four operations available to a double ended queue. They are:
1. pushLeft(): inserts an item to the left end of the queue with the exception that the queue is not full.
2. pushRight(): inserts an item to the right end of the queue with the exception that the queue is not full.
3. popLeft(): removes an item from the left end of the queue with the exception that the queue is not empty.
4. popRight(): removes an item from the right end of the queue with the exception that the queue is not empty.
Now you are given a queue and a list of commands, you have to report the behavior of the queue.
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.Each case starts with a line containing two integers n, m (1 ≤ n ≤ 10, 1 ≤ m ≤ 100), where n denotes the size of the queue and m denotes the number of commands. Each of the next m lines
contains a command which is one of:
pushLeft x pushes x (-100 ≤ x ≤ 100) in the left end of the queue
pushRight x pushes x (-100 ≤ x ≤ 100) in the right end of the queue
popLeft pops an item from the left end of the queue
popRight pops an item from the right end of the queue
Output
For each case, print the case number in a line. Then for each operation, show its corresponding output as shown in the sample. Be careful about spelling.Sample Input | Output for Sample Input |
1 3 8 pushLeft 1 pushLeft 2 pushRight -1 pushRight 1 popLeft popRight popLeft popRight | Case 1: Pushed in left: 1 Pushed in left: 2 Pushed in right: -1 The queue is full Popped from left: 2 Popped from right: -1 Popped from left: 1 The queue is empty |
解:这题的数据不大,开一个数组,从中间开始模拟队列的进出
代码:
/*
l, r 是加是减 用具体几个数试试就清楚了
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int t,k=1,n,m,x,l,r;
int a[30]; //队列已经限制小于10
char str[20];
cin>>t;
while(t--)
{
cout<<"Case "<<k++<<":"<<endl;
int l=r=15;//从中间开始
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>str;
if(str[1]=='u')//进队列的情况
{
cin>>x;
if(r-l>=n)
cout<<"The queue is full"<<endl;
else
{
if(str[4]=='L')
{
cout<<"Pushed in left: "<<x<<endl;
a[l]=x;l--;
}
else
{
cout<<"Pushed in right: "<<x<<endl;
r++;a[r]=x;
}
}
}
else//出队列的情况
{
if(l==r)
cout<<"The queue is empty"<<endl;
else
{
if(str[3]=='L')
{
cout<<"Popped from left: "<<a[l+1]<<endl;
l++;
}
else
{
cout<<"Popped from right: "<<a[r]<<endl;
r--;
}
}
}
}
}
return 0;
}
相关文章推荐
- Lightoj1212——Double Ended Queue(STL)
- C++ Double Ended Queues(双向队列)
- C++ Double Ended Queues(双向队列)
- C++ deque(double-ended-queue)双端队列
- 解码Nginx:双向队列(Queue)
- 【nginx】双向队列(ngx_queue.h)的分割队列(链表) ngx_queue_split操作
- python基础-namedtuple、queue队列、deque双向队列
- Nginx:双向队列(Queue)
- 【nginx】双向队列(ngx_queue.h)的合并队列(链表) ngx_queue_add操作
- Queue 队列分析(单向队列,双向队列,阻塞队列)
- collections之deque【双向队列】与Queue【单向队列】
- STL之queue队列 stack栈 priority_queue优先队列 deque双向队列
- STL:队列(queue)、优先级队列(priority_queue )及双向队列(deque)总结
- nginx双向队列ngx_queue_t使用例子
- Python学习笔记-Day03 -第二部分(双向队列-deque和单向队列Queue)
- Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
- 队列(Queue)--环形队列、优先队列和双向队列
- 栈与队列- STL double_queue
- Nginx 队列双向链表结构 ngx_queue_t
- 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)