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. Hadoop NameNode is not formatted.

    2014-08-26 20:27:22,712 WARN org.apache.hadoop.hdfs.server.namenode.FSNamesystem: Encountered except ...

  2. MySql从服务器延迟解决方案

    在从服务器上执行show slave status;可以查看到很多同步的参数,我们需要特别注意的参数如下:Master_Log_File:                      SLAVE中的I/ ...

  3. 一个用于清除loadrunner产生log文件的批处理

    @echo off set work_path="%~dp0" for /R %%s in (*.txt,*.log) do ( del /f "%%s" ) ...

  4. Android WebView 开发详解(一)

    转载请注明出处  http://blog.csdn.net/typename/article/details/39030091 powered by meichal zhao 概览: Android ...

  5. 执行gem install dryrun错误

    ERROR:  While executing gem ... (Gem::FilePermissionError)     You don't have write permissions for ...

  6. SLF4J user manual

    http://www.slf4j.org/manual.html The Simple Logging Facade for Java (SLF4J) serves as a simple facad ...

  7. springmvc工作流程

    Spring MVC工作流程图   图一   图二    Spring工作流程描述       1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServle ...

  8. 【HDOJ】1462 Word Crosses

    字符串水题,这么做可能比较巧妙. /* 1462 */ #include <iostream> #include <string> #include <map> # ...

  9. poj1724ROADS(BFS)

    链接 本来想写spfa 加点什么限制什么的可能就过了 写着写着就成裸BFS了 也没优化就水过了 #include <iostream> #include<cstdio> #in ...

  10. mapreduce实现全局排序

    直接附代码,说明都在源码里了. package com.hadoop.totalsort; import java.io.IOException; import java.util.ArrayList ...