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 (Java/Others)
Total Submission(s): 4241 Accepted Submission(s):
2164
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!
The first
line has one integer,represent the number oftest cases.
And the input of each
subproblem are described above.
depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any
integer.
#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实现】的更多相关文章
- 用数组模拟STL中的srack(栈)和queue(队列)
我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...
- hdu 1702 ACboy needs your help again!
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1702 ACboy needs your help again! Description ACboy w ...
- hdoj 2 括号配对问题【数组模拟实现+STL实现】
栈遵循先进后出的原则 括号配对问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在,有一行括号序列,请你检查这行括号是否配对. 输入 第一行输入一个数N(0 ...
- uva 12100 Printer Queue 优先级队列模拟题 数组模拟队列
题目很简单,给一个队列以及文件的位置,然后一个一个检查,如果第一个是优先级最高的就打印,否则放到队列后面,求所要打印的文件打印需要花费多长时间. 这里我用数组模拟队列实现,考虑到最糟糕的情况,必须把数 ...
- 数据结构之【栈】+十进制转d进制(堆栈数组模拟)
其实这篇文章开出来主要是水文章%% %% 栈--后进先出的婊 特点:只能在某一端插入和删除的特殊的线性表 操作:进栈--PUSH->向栈顶插入元素 出栈--POP-->将栈顶元素删除 实现 ...
- 简单用数组模拟顺序栈(c++版)适合新手
**栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...
- 简单用数组模拟顺序栈(c++)
**栈是一种操作受限制的线性表,太多官方的话我也不说了,我们都知道栈元素是先进后出的,它有两种存储结构,分别是顺序存储结构和链式存储结构. **今天我先记一下顺序存储结构,后面我会加上链式存储结构的. ...
- Problem UVA12657-Boxes in a Line(数组模拟双链表)
Problem UVA12657-Boxes in a Line Accept: 725 Submit: 9255 Time Limit: 1000 mSec Problem Description ...
- UVA11988-Broken Keyboard(数组模拟链表)
Problem UVA11988-Broken Keyboard Accept: 5642 Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...
随机推荐
- The partner transaction manager has disabled its support for remote/network transactions.
http://technet.microsoft.com/en-us/library/cc753510(WS.10).aspx
- Oracle job procedure
Oracle job procedure 存储过程定时任务 oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 ...
- [转载]C# HashTable 遍历与排序
private void Form1_Load(object sender, EventArgs e) { Hashtable ht = new Hashtable(); ht.Add("j ...
- About GAC
http://blogs.msdn.com/b/msbuild/archive/2007/04/12/new-reference-assemblies-location.aspx http://web ...
- Javascript编程模式(JavaScript Programming Patterns)Part 2.(高级篇)
模块编程模式的启示(Revealing Module Pattern) 客户端对象(Custom Objects) 懒函数定义(Lazy Function Definition) Christian ...
- WAF 与 RASP 的安装使用大比拼!
什么是WAF和RASP? WAF全称是Web application firewall,即 Web 应用防火墙.RASP 全称是 Runtime Application Self-protect,即应 ...
- android 为activity添加optionMenu选项菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android: ...
- 保护模式下pmtest1.asm的理解
整个代码对应内存线性地址分为四段,[gdt] [code32] [video32] [code16] 代码先在实模式[code16]下运行,code16中的cs就是系统分配的该程序物理地址的基址. 编 ...
- [Gauss]POJ2947 Widget Factory
题意: 有n种小工具要加工,每种工具的加工时间为3到9天,给了m条加工记录. 每条记录 X $s_1$ $s_2$ 分别代表 这个工人在$s_1$到$s_2$(前闭后闭)的时间里加工了X件小工具 ...
- HDU 1400 (POJ 2411 ZOJ 1100)Mondriaan's Dream(DP + 状态压缩)
Mondriaan's Dream Problem Description Squares and rectangles fascinated the famous Dutch painter Pie ...