双向队列---1466
2018-01-28 20:08
399 查看
双向队列
[align=center]Time Limit: 1000MS Memory Limit: 65536KB[/align]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
#include <stdio.h> #include <stdlib.h> #include <string.h> //注意在向左和向右添加,删除元素时,方向不一样 int main() { int a[30005],b[10005]; //a数组存测试数据,b数组存不合法命令在a数组中的位置 char s[10]; int i,n,ll=10005,rr=10005,x,y=0; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%s",s); if(strcmp(s,"LIN")==0) { scanf("%d",&x); a[ll]=x; ll--; } else if(strcmp(s,"RIN")==0) { scanf("%d",&x); rr++; a[rr]=x; } else if(strcmp(s,"LOUT")==0) { if(ll>=rr) //当ll>=rr,不合法,ll和rr重置为初始状态,b数组记录 { b[y]=i; y++; ll=10005;rr=10005; } else ll++; } else if(strcmp(s,"ROUT")==0) { if(ll>=rr) //同理 { b[y]=i; y++; ll=10005;rr=10005; } else rr--; } } for(i=ll+1;ll<rr&&i<=rr;i++) { if(i==rr) printf("%d\n",a[i]); else printf("%d ",a[i]); } for(i=0;i<y;i++) printf("%d ERROR\n",b[i]); return 0; }
也可以用c++里stl中的deque:
#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { deque<int>a; vector<int>b; int m,x; string s; cin>>m; for(int i=1; i<=m; i++) { cin>>s; if(s=="LIN") { cin>>x; a.push_front(x); } else if(s=="RIN") { cin>>x; a.push_back(x); } else if(s=="LOUT") { if(a.empty()) b.push_back(i); else a.pop_front(); } else if(s=="ROUT") { if(a.empty()) b.push_back(i); else a.pop_back(); } } for(int i=0; i<a.size(); i++) { if(i!=(a.size()-1)) cout<<a[i]<<' '; else cout<<a[i]<<endl; } for(int i=0; i<b.size(); i++) { cout<<b[i]<<' '<<"ERROR"<<endl; } return 0; }
相关文章推荐
- SDUTOJ(1466)双向队列
- SDUTOJ 1466--双向队列
- SDUT 1466 双向队列
- SDUT-1466 双向队列
- SDUT 1466 双向队列
- SDUT 1466 双向队列
- 暑假集训 8.2 双向队列的手写与STL sdutoj1466 双向队列
- SDUT1466双向队列
- 双向队列 SDUT 1466
- sdut 1466 双向队列
- 1466双向队列
- SDUT OJ 1466 双向队列
- 利用python的双向队列(Deque)数据结构实现回文检测的算法
- 双向队列7.29补
- UVa 11988 破损的键盘 链表 双向队列
- 双向队列
- 双向队列
- python基础-namedtuple、queue队列、deque双向队列
- STL系列之一 deque双向队列
- HDU5933 +(双向队列)2016年中国大学生程序设计竞赛(杭州)