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. FreeBsd网络性能优化方案sysctl

    以下是阿盛的配置 sysctl net.inet.tcp.msl= sysctl net.inet.tcp.mssdflt= sysctl net.inet.tcp.minmss= sysctl ne ...

  2. Gitlab CI 持续集成的完整实践

    Gitlab CI 持续集成的完整实践 本着公司团队初创,又在空档期想搞点事情,搭建了私有Gitlab的契机,顺便把持续集成搭建起,实现了对Python服务端代码的单元测试.静态代码分析和接口测试的持 ...

  3. 第18课 类型萃取(2)_获取返回值类型的traits

    1. 获取可调用对象返回类型 (1)decltype:获取变量或表达式的类型(见第2课) (2)declval及原型 ①原型:template<class T> T&& d ...

  4. Struts2 中常用的代码

    BaseAction public class BaseAction extends ActionSupport { protected String target; public Map getRe ...

  5. JavaWeb——java.lang.UnsatisfiedLinkError

    java.lang.UnsatisfiedLinkError: Native Library sqlite-3.8.6-amd64-sqlitejdbc.dll already loaded in a ...

  6. java GC是在什么时候,对什么东西,做了什么事情

    面试题:“你能不能谈谈,java GC是在什么时候,对什么东西,做了什么事情?” 面试题目:地球人都知道,Java有个东西叫垃圾收集器,它让创建的对象不需要像c/cpp那样delete.free掉,你 ...

  7. ASCS HA

    Please let us know what do you mean by "the PAS can not be accessed", what error did you f ...

  8. 分享下自己写的一个微信小程序请求远程数据加载到页面的代码

    1  思路整理 就是页面加载完毕的时候  请求远程接口,然后把数据赋值给页面的变量 ,然后列表循环 2 js相关代码  我是改的 onload函数 /** * 生命周期函数--监听页面加载 */ on ...

  9. spring 之 BeanPostProcessor

    粗略一看, 它有这么多实现: 可见, 它是多么基础 而重要的一个 接口啊! 它提供了两个方法: public interface BeanPostProcessor { Object postProc ...

  10. python3 发送邮件

    import smtplibfrom email.mime.text import MIMETextdef SendEmail(fromAdd,toAdd,subject,text): _pwd = ...