ACboy was kidnapped!! 
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(. 
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy." 
The problems of the monster is shown on the wall: 
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out"). 
and the following N lines, each line is "IN M" or "OUT", (M represent a integer). 
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

InputThe input contains multiple test cases. 
The first line has one integer,represent the number oftest cases. 
And the input of each subproblem are described above.OutputFor each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.Sample Input

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

Sample Output

1
2
2
1
1
2
None
2
3
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 50//储存空间初始分配量
#define STACKINCREMENT 10//存储空间分配增量
#define Queue_MAX_SIZE 50
#define OK 1
#define ERROR 0
typedef int QueueType; //队列元素类型
typedef int StackType; //栈元素类型 typedef struct
{
QueueType *pBase; //队列指针
QueueType front; //队头索引
QueueType rear; //队尾索引
int maxSize; //当前分配最大容量
}Queue;
//队列的初始化
int InitQueue(Queue *p)
{
p->pBase = (QueueType *)malloc(Queue_MAX_SIZE * sizeof(QueueType));
if (p->pBase == NULL) return ERROR; //内存分配失败
p->front = ;
p->rear = ; //初始化 队头队尾索引均为0
p->maxSize = Queue_MAX_SIZE;
return ;
}
//销毁队列
void DestroyQueue(Queue *p)
{
free(p);
p = NULL; }
//清空队列
void ClearQueue(Queue *p)
{
p->front = ;
p->rear = ;
}
//判断队列是否为空
int IsEmpityQueue(Queue *p)
{
if (p->front == p->rear)
return OK;
return ERROR; } //判断队列是否满
int IsFullQueue(Queue *p)
{
if ((p->rear + ) % p->maxSize == p->front)
return OK;
return ERROR; }
//新元素入队
int EnQueue(Queue *p, QueueType e)
{
if (IsFullQueue(p) == OK)
{
printf("队列已满\n");
return ERROR;
}
p->pBase[p->rear] = e;
p->rear = (p->rear + ) % p->maxSize;
return OK;
}
//队头元素出列
int DeQueue(Queue *p, QueueType *pe)
{
//如果队列为空 则返回ERROR
if (IsEmpityQueue(p) == OK)
return ERROR; *pe = p->pBase[p->front];
p->front = (p->front + ) % p->maxSize; return OK;
} typedef struct {
StackType *base; //在构造之前和销毁之后,base的值为NULL
StackType *top; //栈顶指针
int stacksize; //当前已分配的存储空间,以元素为单位
}SqStack; //顺序栈 //栈的初始化
int InitStack(SqStack *p)
{
p->base = (StackType*)malloc(STACK_INIT_SIZE * sizeof(StackType));
if (p->base == NULL) return ERROR; //内存分配失败
p->top = p->base; //栈顶与栈底相同表示一个空栈
p->stacksize = STACK_INIT_SIZE;
return OK; }
//判断栈是否为空
int EmptyStack(SqStack *p) {
//若为空栈 则返回OK,否则返回ERROR
if (p->top == p->base) return OK;
else
return ERROR;
}
//顺序栈的压入
int Push(SqStack *p, StackType e) {
//插入元素e为新的栈顶元素
if ((p->top - p->base) >= p->stacksize) //栈满,追加储存空间
{
p->base = (StackType*)realloc(p->base, (p->stacksize + STACKINCREMENT) * sizeof(StackType));
if (p->base == NULL) return ERROR;// 储存空间分配失败
p->top = p->base + p->stacksize; //可能有人觉得这句有点多余(我当时也是这么想的 后面有解释)
p->stacksize += STACKINCREMENT;
}
*(p->top) = e;
(p->top)++;
return OK;
}
// 顺序栈的弹出
int Pop(SqStack *p, StackType *e) {
//若栈不空,则删除p的栈顶元素,用e返回其值
if (EmptyStack(p) == OK)
return ERROR; --(p->top);
*e = *(p->top);
return OK; }
//顺序栈的销毁
int DestroyStack(SqStack *p) {
//释放栈底空间并置空
free(p->base);
p->base = NULL;
p->top = NULL;
p->stacksize = ; return OK;
}
//将顺序栈置空 栈还是存在的,栈中的元素也存在,如果有栈中元素的地址任然能调用
int ClearStack(SqStack *p) {
p->top = p->base;
return OK;
} int main()
{
int result[],k=;
StackType *se, st;
SqStack *pstack, stack;
pstack = &stack;
se = (StackType*)malloc(sizeof(StackType)); //为指针se分配内存地址 QueueType *qe = (QueueType*)malloc(sizeof(QueueType)),qt;
Queue *pQueue = (Queue *)malloc(sizeof(Queue));
InitStack(pstack); //初始化栈
InitQueue(pQueue); //初始化队列
int x,y;
char flag[],handle[];
scanf("%d", &x);
for (int i = ; i < x; i++)
{
scanf("%d", &y);
scanf("%s", flag);
for (int j = ; j < y; j++)
{
if (strcmp(flag, "FIFO")==) //队列
{
scanf("%s", handle);
if (strcmp(handle, "IN") == ) { scanf("%d", &qt); EnQueue(pQueue, qt); }
if (strcmp(handle, "OUT") == )
{
if(DeQueue(pQueue, qe)==OK) result[k++]=*qe;
else result[k++] = ; //99是一个标记 } } if (strcmp(flag, "FILO") == ) //栈
{
scanf("%s", handle);
if (strcmp(handle, "IN") == ) { scanf("%d",&st ); Push(pstack, st); }
if (strcmp(handle, "OUT") == )
{
if(Pop(pstack,se)==OK) result[k++]=*se;
else
result[k++] = ;
}
}
}
ClearStack(pstack);
ClearQueue(pQueue); }
result[k] = '\0';
DestroyQueue(pQueue);
DestroyStack(pstack);
k = ;
while (result[k] != '\0')
{
if (result[k] != )
printf("%d\n", result[k]); else
printf("None\n"); k++;
}
return ; }

A - ACboy needs your help again!的更多相关文章

  1. hdu 1702 ACboy needs your help again!

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1702 ACboy needs your help again! Description ACboy w ...

  2. (hdu step 8.1.1)ACboy needs your help again!(STL中栈和队列的基本使用)

    题目: ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...

  3. hdu 1712 ACboy needs your help

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. hdu 1712 ACboy needs your help 分组背包

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 Problem ...

  5. hdoj 1702 ACboy needs your help again!【数组模拟+STL实现】

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. HDU 1712 ACboy needs your help 典型的分组背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1712 ACboy needs your help Time Limit: 1000/1000 MS ( ...

  7. ACboy needs your help(HDU 1712 分组背包入门)

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. ACboy needs your help again!--hdu1702

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. 【泛化物品】【HDU1712】【ACboy needs your help】

    ACboy needs your help Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  10. HDU 1712 ACboy needs your help(包背包)

    HDU 1712 ACboy needs your help(包背包) pid=1712">http://acm.hdu.edu.cn/showproblem.php? pid=171 ...

随机推荐

  1. 廖雪峰Java6 IO编程-2input和output-7序列化

    1.序列化 序列化是指把一个Java对象变成二进制内容byte[] 序列化后可以把byte[]保存到文件中 序列化后可以把byte[]通过网络传输 一个Java对象要能序列化,必须实现Serializ ...

  2. Scrapy实战篇(五)之爬取历史天气数据

    本篇文章我们以抓取历史天气数据为例,简单说明数据抓取的两种方式: 1.一般简单或者较小量的数据需求,我们以requests(selenum)+beautiful的方式抓取数据 2.当我们需要的数据量较 ...

  3. [UE4]Tree View

    类似List View,但Tree View要求提供树形结构的数据.Tree View和Tile View都是继承自List View 一.创建一个名为“TreeEntry”的UserWidget,添 ...

  4. 用GDB调试程序(二)

    GDB的命令概貌——————— 启动gdb后,就你被带入gdb的调试环境中,就可以使用gdb的命令开始调试程序了,gdb的命令可以使用help命令来查看,如下所示: /home/hchen> g ...

  5. HTTP的长连接(持久连接)和短连接

      HTTP的长连接和短连接 本文总结&分享网络编程中涉及的长连接.短连接概念. 关键字:Keep-Alive,并发连接数限制,TCP,HTTP 一.什么是长连接 HTTP1.1规定了默认保持 ...

  6. python 在window 系统 连接并操作远程 oracle 数据库

    1,python 连接 oracle 需要 oracle 自身的客户端  instantclient,可以去官网下载自己需要的版本, https://www.oracle.com/technetwor ...

  7. SpringMvc 使用Thumbnails压缩图片

    ```java @PostMapping(value = "/upLoadFile") @ApiOperation(value = "上传文件") public ...

  8. PHPMailer出现SMTP connect() failed.

    很可能是端口问题,最好把$mailer->SMTPSecure和$mailer->Port分别设置为ssl与465或者tls与587,否则某些浏览器不接受不安全的链接,导致$mailer- ...

  9. Flannel网络插件配置

    # ps -ef|grep docker|grep bip FLANNEL_OPTIONS="-ip-masq=true" -ip-masq=true 这个参数的目的是让flann ...

  10. python装饰器补漏

    以前写过一篇装饰器文章,觉得少了点东西,今天特来补上,也就是带参数的装饰器,上篇文章写的不严谨 def logger(logs=""): def outer(f): def inn ...