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 ...
随机推荐
- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试]:思考题——谢勤政11061197
第一题: 大楼里面的电梯一般分区域,或考虑思考题第四题的情况,运行楼层不一样的电梯属于不同的区域.然后在接口IRequest和IPassenger还有IElevator里面都加上int area这个属 ...
- sql关键查询
情境:保留表A数据,且A表与B表是一对多关系 SELECT tuf.Id,tuf.FileName,tuf.type,tuf.url,tum.MachineId,tum.IsDownland,tum. ...
- 学无止境,学习AJAX(二)
POST 请求 一个简单 POST 请求: xmlhttp.open("POST","demo_post.asp",true); xmlhttp.send(); ...
- [DP] Rod-cutting problem
给一个长度为 n 的杆子,切成小段卖出去,价格根据小段的长度不同而不同.下面是一个例子 我们要通过切成小段卖出尽可能高的总价钱.问题是:How to decompose the problem? De ...
- Win7下安装Mongodb教程
最新Mongodb下载地址:http://www.mongodb.org/downloads 1.下载完成后,解压到任意路径,如:D:\mongodb: 2.在D:\mongodb目录下 新建data ...
- Codeforces Round #209 (Div. 2)
A: 要么是两次要么4次,判断是否在边界: #include<cstdio> using namespace std; int main() { int n,m,x; ; scanf(&q ...
- [Gauss]POJ2947 Widget Factory
题意: 有n种小工具要加工,每种工具的加工时间为3到9天,给了m条加工记录. 每条记录 X $s_1$ $s_2$ 分别代表 这个工人在$s_1$到$s_2$(前闭后闭)的时间里加工了X件小工具 ...
- 李洪强漫谈iOS开发[C语言-017]-printf函数
- 网络基本功(八):细说TCP滑动窗口
https://community.emc.com/message/842129#842129
- !!Python字典增删操作技巧简述+Python字典嵌套字典与排序
http://developer.51cto.com/art/201003/186006.htm Python编程语言是一款比较容易学习的计算机通用型语言.对于初学者来说,首先需要掌握的就是其中的一些 ...