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. Core管道中的处理流程3

    通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[下]:管道是如何构建起来的? 在<中篇>中,我们对管道的构成以及它对请求的处理流程进行了详细介绍,接下 ...

  2. CSS的绝对定位和相对定位

    css定位标签position包括两个值:relative(相对定位)和absolute(绝对定位),position样式一般都是和top.bottom.left.right一起使用来确定一个标签的位 ...

  3. CSS水平、垂直居中小结

    我们在实际工作中经常会遇到需要水平居中或者垂直居中的场景,今天我们就来看一下如何设置水平居中和垂直居中. 首先来看水平居中. 一.水平居中设置-行内元素. 如果被设置元素为文本.图片等行内元素时,水平 ...

  4. Hbase region 某个regionserver挂掉后的处理

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwoAAACdCAMAAAAjbX91AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK

  5. 简单方法打包.net程序集脱离framework

    最近业余捣鼓monogame,自然而然就关注到了.net程序脱离framework发布的问题上了, 度娘,谷歌娘 都经过一番查找,无非分为如下几类方法: 1.直接使用mono运行时,附带 bin.li ...

  6. Android ListView(Selector 背景图片 全选 Checkbox等按钮)

    list_item.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xm ...

  7. HTTP Header 入门详解

    什么是HTTP Headers HTTP是"Hypertext Transfer Protocol"的所写,整个www都在使用这种协定,几乎你在流览器里看到的大部分内容都是通过ht ...

  8. ArcGIS Engine生成等值线(C#)

    原文:ArcGIS Engine生成等值线(C#) 本文介绍c#写的利用ArcGIS Engine生成等值线的方法.c#写的根据雨量站的降雨量值内插出降雨量等值线的功能.做几点说明:根据离散点生成等值 ...

  9. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  10. ide远程调试

    这篇写得好:http://qifuguang.me/2015/09/18/IntelliJ%E8%BF%9C%E7%A8%8B%E8%B0%83%E8%AF%95%E6%95%99%E7%A8%8B/