ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4241    Accepted Submission(s):
2164

Problem Description
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!
 
Input
The 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.
 
Output
For 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<string.h>
int main()
{
int n,m,j,i,sum,t,k,top;
char a[5]={"FIFO"};
char b[5]={"FILO"};
char d[5]={"IN"};
char e[5]={"OUT"};
char s[5];
char c[5];
int zhan[1100];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s",s);
top=0;k=0;
for(i=0;i<n;i++)
{
scanf("%s",c);
if(strcmp(c,d)==0) //如果输入的字符串为IN
{
scanf("%d",&m); //则输入其后的数字并存入栈中
zhan[top++]=m;
}
else if(strcmp(c,e)==0) //如果输入字符串为OUT
{
if(strcmp(s,b)==0) //则判断字符串s是 FIFO还是FILO即是先进先出还是先进后出
{ //此处为先进后出
if(top>0) //如果栈不为空
{
printf("%d\n",zhan[top-1]);//则输出栈尾元素
top--; //并删除此元素
}
else if(top<=0)
printf("None\n");//如果栈为空则输出None
}
else if(strcmp(s,a)==0)
{
if(k<top) //如果k<top即栈不为空
{
printf("%d\n",zhan[k++]);//输出栈首元素并删除
}
else if(k>=top) //如果栈为空
printf("None\n");//则输出None
}
}
}
}
return 0;
}

  STL:

#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#define MAX 1100
using namespace std;
char s1[MAX];
char str1[5]={"IN"};
char str4[5]={"FILO"};
int main()
{
int n,m,j,i,t,k,l;
int a,b;
char str[MAX];
stack<int>s;
queue<int>q;
scanf("%d",&t);
getchar();
while(t--)
{
scanf("%d%s",&n,str);
a=0;
if(strcmp(str,str4)==0)
{
while(n--)
{
scanf("%s",s1);
//scanf("%s %d",s1,&m);
if(strcmp(s1,str1)==0)
{
scanf("%d",&m);
s.push(m);
}
else
{
if(s.empty())
{
printf("None\n");
continue;
}
a=s.top();
printf("%d\n",a);
s.pop();
}
}
while(!s.empty())
{
s.pop();
}
}
else
{
while(n--)
{
scanf("%s",s1);
//scanf("%s %d",s1,&m);
if(strcmp(s1,str1)==0)
{
scanf("%d",&m);
q.push(m);
}
else
{
if(q.empty())
{
printf("None\n");
continue;
}
a=q.front();
printf("%d\n",a);
q.pop();
}
}
while(!q.empty())
q.pop();
}
}
return 0;
}

  

hdoj 1702 ACboy needs your help again!【数组模拟+STL实现】的更多相关文章

  1. 用数组模拟STL中的srack(栈)和queue(队列)

    我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...

  2. hdu 1702 ACboy needs your help again!

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

  3. hdoj 2 括号配对问题【数组模拟实现+STL实现】

    栈遵循先进后出的原则 括号配对问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在,有一行括号序列,请你检查这行括号是否配对.   输入 第一行输入一个数N(0 ...

  4. uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列

    题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...

  5. 数据结构之【栈】+十进制转d进制(堆栈数组模拟)

    其实这篇文章开出来主要是水文章%% %% 栈--后进先出的婊 特点:只能在某一端插入和删除的特殊的线性表 操作:进栈--PUSH->向栈顶插入元素 出栈--POP-->将栈顶元素删除 实现 ...

  6. 简单用数组模拟顺序栈(c++版)适合新手

    **栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...

  7. 简单用数组模拟顺序栈(c++)

    **栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...

  8. Problem UVA12657-Boxes in a Line(数组模拟双链表)

    Problem UVA12657-Boxes in a Line Accept: 725  Submit: 9255 Time Limit: 1000 mSec Problem Description ...

  9. UVA11988-Broken Keyboard(数组模拟链表)

    Problem UVA11988-Broken Keyboard Accept: 5642  Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...

随机推荐

  1. .net faq

    http://www.indiabix.com/technical/dotnet/ http://www.codeproject.com/Articles/637480/Csharp-and-ASP- ...

  2. 移动端Reactive Native轮播组件

    移动端Reactive Native轮播组件 总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reac ...

  3. python 操作mongodb数据库模糊查询

    # -*- coding: utf-8 -*-import pymongoimport refrom pymongo import MongoClient #创建连接#10.20.66.106clie ...

  4. oct(x) 将一个数字转化为8进制

    >>> a = 12 >>> b 21 >>> c = oct(a) >>> d = oct(b) >>> c ...

  5. location.search 详解

    JS中location.search什么意思 设置或获取网页地址跟在问号后面的部分 当以get方式在url中传递了请求参数时,可以利用location的search属性提取参数的值,下面的代码把参数的 ...

  6. 关于JDNI、JMX

    http://www.cnblogs.com/itech/archive/2010/09/16/1827999.html http://javacrazyer.iteye.com/blog/75948 ...

  7. String类的split方法以及StringTokenizer

    split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...

  8. RubyCritic:一款不错的检测代码质量工具

    关注代码质量是高效开发必须要做的一件事,那么在 Ruby 开发的过程中,是否有什么好的代码质量检测工具呢?下面由 Ruby 工程师路英瑞介绍一下 RubyCritic--一款还不错的代码质量检测工具. ...

  9. [dp]Codeforces30C Shooting Gallery

    题目链接 题意: 给n个点 每个点的坐标 x y 出现的时间t 射中的概率 从i点到j点的时间为它们的距离. 求射中个数的最大期望 很水的dp  坑点就是要用LL #include <cstdi ...

  10. 第四章 USB库介绍

    4.1 USB库函数简介 Luminary Micro公司提供USB处理器的USB库函数,应用在Stellaris处理器上,为USB设备.USB主机.OTG开发提供USB协议框架和API函数,适用于多 ...