Problem Description
refresh最近发了一笔横财,开了一家停车场。由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先
进入停车场,而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道,
Del 表示停车场中出去了一辆车,Out 表示便道最前面的车辆不再等待,放弃进入停车场)。假设便道内的车辆不超过1000000.
Input
输入为多组数据,每组数据首先输入N和M(0< n,m <200000),接下来输入M条命令。
Output
输入结束后,如果出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆,最后进入的最先输出,无车辆不输出。
Sample Input
2 6 Add 18353364208 Add 18353365550 Add 18353365558 Add 18353365559 Del OutSample Output
18353365558 18353364208Hint
Source
#include<stdio.h> #include<stdlib.h> #include<string.h> char a[200005][25] ; //栈 int top ; char b[1000001][25] ; //队列 int top1 ,front ; int main() { int n , m ; int i ; char o[6]; char num[25] ; while(~scanf("%d %d",&n,&m)){ int flag = 1 ; top = -1 ; front = top1 = -1 ; while(m--){ scanf("%s",o) ; if(strcmp(o,"Add") == 0){ scanf("%s",num) ; if(top<n-1){ strcpy(a[++top],num) ; } else { if(top1 == front ){ front++ ; top1++ ; } strcpy(b[++top1],num) ; } } else if(strcmp(o,"Del") == 0){ if(top==-1){ flag = 0 ; } else { top-- ; if(top1!=front){ strcpy(a[++top],b[++front]) ; } } } else if(strcmp(o,"Out") == 0){ if(front == top1){ flag = 0 ; } else { front++ ; } } } if(flag){ for(i = top ; i>=0 ; i--){ printf("%s\n",a[i]) ; } } else printf("Error\n") ; } return 0 ; }代码2:
#include<stdio.h> #include<string.h> #include<stdlib.h> //定义队列 typedef struct node { char num[100] ; struct node *next ; } NODE; //定义栈 char a[200000][100] ; int top ; int main() { int n, m; int i ; char ch[100] ; char str[100] ; NODE *rear, *front ; //定义队列的头结点和尾结点 int tag ; while(~scanf("%d %d",&n,&m)) { NODE *head = (NODE*)malloc(sizeof(NODE)) ; NODE *p ; head -> next = NULL ; rear = head ; front = head ; tag = 1 ; top = -1 ; while(m--) { scanf("%s",ch) ; if(strcmp(ch,"Add") == 0 ) { scanf("%s",str) ; if(top<n-1) { strcpy(a[++top],str) ; } else { p = (NODE*)malloc(sizeof(NODE)) ; strcpy(p->num,str) ; rear->next = p ; rear = p ; } } else if(strcmp(ch,"Del") == 0) { if(top==-1) { tag = 0 ; } else { top-- ; if(rear!=front) { p = front->next ; front->next = p->next ; if(p->next == NULL) { rear = front ; } strcpy(a[++top],p->num) ; free(p) ; } } } else { if(front == rear) { tag =0 ; } else { p = front -> next ; front->next = p->next ; if(p->next == NULL) { rear = front ; } free(p) ; } } } if(tag) { for(i = top ; i >=0 ; i--) { printf("%s\n",a[i]) ; } } else printf("Error\n") ; } return 0 ; }#include<stdio.h> #include<string.h> #include<stdlib.h> //构建队列 typedef struct node { char num[100] ; struct node *next ; }NODE ; typedef struct { NODE *rear; NODE *front ; }Link ; //构建栈 char a[1000001][100] ; int top ; int main() { int n , m ; int i ; NODE *p ; Link l ; l.front = (NODE*)malloc(sizeof(NODE)) ; l.rear = l.front ; char num[100] ; char op[10] ; while(~scanf("%d %d",&n,&m)) { int tag = 1 ; top = -1 ; while(m--) { scanf("%s",op) ; if(strcmp(op,"Add") == 0) { scanf("%s",num) ; if(top < n-1) { strcpy(a[++top],num) ; } else { p = (NODE *)malloc(sizeof(NODE)) ; strcpy(p->num,num) ; p->next = NULL ; l.rear->next = p ; l.rear = p ; } } else if(strcmp(op,"Del") == 0) { if(top>=0) { top-- ; if(l.front==l.rear) { tag = 0 ; } else { strcmp(a[++top],l.front->next->num) ; p = l.front->next ; l.front->next = p ; if(p->next == NULL) { l.front = l.rear ; } free(p) ; } } } else { if(l.front==l.rear) { tag = 0 ; } else { p = l.front->next ; l.front->next = p ; if(p->next == NULL) { l.front = l.rear ; } free(p) ; } } } if(tag){ for(i=top ; i>=0 ;i--) { printf("%s\n",a[i]) ; } } else printf("Error\n") ; } return 0 ; }