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. [转]检测到有潜在危险的 Request.Form 值

    <system.web> <httpRuntime targetFramework="4.0" requestValidationMode="2.0&q ...

  2. Centos7 安装redis集群哨兵模式

    https://blog.csdn.net/lihongtai/article/details/82826809

  3. Linux find 命令参数大全及示例

    Linux中find常见用法示例 命令格式:find path -option [-print] [ -exec -ok command] {} \; 参数说明: path:find命令所查找的目录路 ...

  4. [sql]sql函数coalesce返回第一个非空的值

    下面来看几个比较有用的例子: 首先,从MSDN上看看这个函数的使用方法,coalesce函数(下面简称函数),返回一个参数中非空的值.如: SELECT  COALESCE(NULL, NULL, G ...

  5. sql server 数据库变成单用户模式的恢复

    USE master;GODECLARE @SQL VARCHAR(MAX);SET @SQL=''SELECT @SQL=@SQL+'; KILL '+RTRIM(SPID)FROM master. ...

  6. Window10下安装sbt

    参考:https://segmentfault.com/a/1190000002474507 下载:https://dl.bintray.com/sbt/native-packages/sbt/0.1 ...

  7. (整理)SQL Server 2008 CDC 功能使用

    最近某项目突然要增加数据的获取,但是不能改程序.也没有同步的只读库,只好使用CDC来进行尝试. CDC的启用和停止全部用SQL实现,在这里给出主要的SQL步骤: /****** Script for ...

  8. Vue keep-alive的总结

    1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗. <keep-alive> <component> <!- ...

  9. java面试题收集

    http://www.cnblogs.com/yhason/archive/2012/06/07/2540743.html 2,java常见面试题 http://www.cnblogs.com/yha ...

  10. 44个Java代码性能优化总结

    https://blog.csdn.net/xiang__liu/article/details/79321639 ---稍后有时间整理