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. percona-toolkit(pt-online-schema-change)工具包的安装和使用

    1.下载和安装percona toolkit的包 #yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/ ...

  2. .NET自动化测试工具链:Selenium+NUnit+ExtentReport

    Selenium可以执行UI的交互,ExtentReport用来生成测试报告,NUnit是我熟悉的基础测试框架,当然你也可以用MSTest.Xunit来代替.Selenium.NUnit没啥好讲的,网 ...

  3. vuex状态管理-数据改变不刷新

    困惑: 在页面初始化的时候,我提交到vuex状态管理,然后在获取的时候获取不到,我找到了出错的地点,并进行了修改,然后可以获取到状态 但是不知道原因? 定义了如下的state const state ...

  4. Sqlserver 2016 R Service环境安装的各种错误(坑)解决办法

    相信很多朋友都会慕名Sqlserver 2016的R语言功能,将自己的数据库升级到Sqlserver 2016,但是当你安装完Sqlserver 2016的R语言组件之后,你会发现并不能直接使用,比如 ...

  5. mysql相关碎碎念

    取得当天: SELECT curdate(); mysql> SELECT curdate();+------------+| curdate()  |+------------+| 2013- ...

  6. Oracle 学习笔记 (七)

    一.数据库的启动 启动数据库的三个阶段: nomount, mount,open mount 阶段:. 1.读参数文件 2.分配内存 3.启动后台进程 4.初始化部分v$视图 mount 阶段: 读参 ...

  7. Spring MVC Controller异常处理总结

    在项目开发过程中,经常遇到服务被攻击的情况,虽然接口在设计过程中有相当多的安全措施,例如cookie校验.风控.访问熔断等相关技术保证服务的安全性,不过感觉还是有必要收集分析一下这些攻击请求者,以备为 ...

  8. 安装grid时找不到ASM共享磁盘

    1.安装ORACLE数据库集群软件grid时找不到共享磁盘,如下图: 2.网上找过有各种说法,但此处小编的解决方案是:通过重新安装软件:oracleasmlib-2.0.4-1.el6.x86_64. ...

  9. kettle 常用组件

    Dummy步骤不会做任何事情.它的主要功能是作为以测试为目的的占位符. 追加流 分析查询(前后行查询),步骤:增加常量数据包括id,name连个字段,增加自增列autoid字段,按id,outid进行 ...

  10. C#发送QQ邮件

    1.首先配置一下发件人的账号密码(密码根据自己所选择的的邮箱填写,此处不做展示) <?xml version="1.0" encoding="utf-8" ...