双向队列
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 <string.h> int main() { int a[100002],b[10002]; char c[200]; int i,j,n,top=0; while(~scanf("%d",&n)){ memset(b,0,sizeof(b)); top = 0; for(j =1;j <= n;j++) { scanf("%s",c); if(strcmp("LIN",c) == 0) { top++; if(top!=1) { for(i = top;i >= 1;i--) a[i] = a[i-1]; } scanf("%d",&a[0]); } else if(strcmp("RIN",c) == 0) { scanf("%d\n",&a[top]); top++; } else if(strcmp("LOUT",c) == 0) { if(top>0) { for(i = 0;i < top;i++) { a[i] = a[i+1]; } top--; } else b[j] = -1; } else if(strcmp("ROUT",c) == 0) { if(top>0) top--; else b[j] = -1; } } for(i = 0;i < top;i++) { if(i < top-1) printf("%d ",a[i]); else printf("%d\n",a[i]); } for(i = 1;i <= n;i++) { if(b[i] == -1) printf("%d ERROR\n",i); }} return 0; }