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. linux下误删目录文件后恢复神器extundelete

    原文链接:https://blog.51cto.com/wzlinux/2052835 参考:https://blog.csdn.net/cwg_1992/article/details/463100 ...

  2. Program type already present: android.support.v4.widget.EdgeEffectCompat

    1.确保所有依赖包的 implementation 'com.android.support:appcompat-v7:25.4.0'是一样的 2.确保最外层的build.gradle中增加如下代码: ...

  3. 《女神异闻录 5》的 UI 设计

    转自:https://www.zhihu.com/question/50995871?sort=created <女神异闻录5>是近两年最为火热的JRPG游戏之一,它的出色不仅在于剧情暗讽 ...

  4. RabbitMQ--windows10环境下的RabbitMQ安装步骤(转)

    https://blog.csdn.net/weixin_39735923/article/details/79288578

  5. js数组条件筛选——map()

    在对象数组中检索属性为指定值得某个对象使用map()就非常方便. 对象数组 var studentArray = [ {"name":"小明","ge ...

  6. SpringBoot 六问

    1.什么是springboot         用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件)                  创建独立 ...

  7. ELK+Beats日志分析系统部署

    一.            名词介绍: E:ElasticSearch 搜索,简称es L:Logstash 管理日志和事件的工具 K:Kibana 功能强大的数据显示客户端 Beats 轻量级数据传 ...

  8. input输入限制

    1:只能输入两位小数点:function keepTwoPointNum(that){ var val=that.value; if(isNaN(val)){ $(that).val(''); ret ...

  9. 远程桌面 把explorer关掉了

    用Ctrl+Alt+End调出远程桌面的任务管理器.然后,运行explorer.exe即可重启该服务.

  10. thinkphp5.0 - Redis 实现秒杀

    首先,因为秒杀这个环节在商城项目中比较常见,最近写商城项目,碰到这个功能模块,于是就拿出来给大家分享一波. 难点:高并发的情况下,正常逻辑写的话数据库的库存会出现负数,对付这类问题有很多解决方案,我就 ...