双向队列
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 i,t,k,j,top,x,a[10002],f[10003]; char s[6]; scanf("%d",&t); memset(f,0,sizeof(f)); top=0; for(k=1; k<=t; k++) { scanf("%s",s); if(strcmp(s,"LIN")==0) { scanf("%d",&x); top++; if(top!=0) for(i=top; i>=1; i--)//一定要反过来,不然数据会丢失 a[i]=a[i-1]; a[1]=x; } if(strcmp(s,"RIN")==0) { scanf("%d",&x); top++; a[top]=x; } if(strcmp(s,"LOUT")==0) { if(top!=0) { for(j=1; j<=top; j++) a[j]=a[j+1]; top--; } else f[k]=1; } if(strcmp(s,"ROUT")==0) { if(top!=0) { top--; } else f[k]=1; } } if(top!=0) {for(i=1; i<top; i++) { printf("%d ",a[i]); } printf("%d\n",a[top]);} for(i=1; i<=t; i++) if(f[i]==1) printf("%d ERROR\n",i); return 0; }