hdu1702(ACboy needs your help again!) 在杭电又遇坑了
2015-08-10 00:53
387 查看
点击打开链接
pop 操作,以及取堆栈顶点的 peek 方法、测试堆栈是否为空的 empty 方法、在堆栈中查找项并确定到堆栈顶距
离的 search 方法。
构造方法:
方法:
主要的方法:
he miss his mother very much and is very scare now.You can't image how dark the room he was putinto 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 ainteger 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 ainteger).
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 oneinteger,represent the number oftest cases.
And the input of each subproblem are described above.
结题感悟:
其实吧,这题并不是很难,就是一个栈和队列的公共题,也就是按指定的方式(栈或队列)存取数据,但是为什么我自己写的栈和队列就是不能再杭电ac(一直wa啊),而用java包中的栈和队列就秒过了,问题尚未找出原因,值得思考啊。不过可以趁此学学这两个类(尽量还是自己动手写的好啊)栈:java.util 类 Stack<E>
Stack类表示后进先出(LIFO)的对象堆栈。它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈。它提供了通常的push 和
pop 操作,以及取堆栈顶点的 peek 方法、测试堆栈是否为空的 empty 方法、在堆栈中查找项并确定到堆栈顶距
离的 search 方法。
构造方法:
[b]Stack() 创建一个空堆栈。[/b]
方法:
boolean | empty() 测试堆栈是否为空。 |
E | peek() 查看堆栈顶部的对象,但不从堆栈中移除它。 |
E | pop() 移除堆栈顶部的对象,并作为此函数的值返回该对象。 |
E | push(E item) 把项压入堆栈顶部。 |
int | search(Object o) 返回对象在堆栈中的位置,以 1 为基数。 |
队列:java.util 接口 Queue<E>。这是一个接口不能直接使用,所以这里只能找它实现类ArrayDeque
构造方法:ArrayDeque() 构造一个初始容量能够容纳 16 个元素的空数组双端队列。 |
ArrayDeque(Collection<? extendsE> c) 构造一个包含指定 collection 的元素的双端队列,这些元素按 collection 的迭代器返回的顺序排列。 |
ArrayDeque(int numElements) 构造一个初始容量能够容纳指定数量的元素的空数组双端队列。 |
void | addFirst(E e) 将指定元素插入此双端队列的开头。 |
void | addLast(E e) 将指定元素插入此双端队列的末尾。 |
E | pop() 从此双端队列所表示的堆栈中弹出一个元素。 |
void | push(E e) 将元素推入此双端队列所表示的堆栈。 |
boolean | isEmpty() 如果此双端队列未包含任何元素,则返回 true。 |
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 putinto 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 ainteger 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 ainteger).
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 oneinteger,represent the number oftest cases.
And the input of each subproblem are described above.
Output
For each command "OUT", you should output ainteger depend on the word is "FIFO" or "FILO", or a word "None" if you don't have anyinteger.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
直接用java包中的类:
package stack;
import java.util.ArrayDeque;
import java.util.Scanner;
import java.util.Stack;
public class P1702_2 {
public staticvoid main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String mode, operate;
int num, a;
while (t-- > 0) {
int n = sc.nextInt();
mode = sc.next();
if (mode.charAt(2) == 'F') {
ArrayDeque queue=new ArrayDeque(n);
for (int i = 0; i < n; i++) {
operate = sc.next();
if (operate.charAt(0) == 'I') {
num = sc.nextInt();
queue.addLast(num);
} else {
if (queue.isEmpty()) {
System.out.println("None");
} else {
System.out.println(queue.pop());
}
}
}
} else {
Stack stack = new Stack();
for (int i = 0; i < n; i++) {
operate = sc.next();
if (operate.charAt(0) == 'I') {
num = sc.nextInt();
stack.push(num);
} else {
if (stack.isEmpty()) {
System.out.println("None");
} else {
System.out.println(stack.pop());
}
}
}
}
}
}
}
自己写的类(数据都能过,就是不能ac):
package stack;
import java.util.Scanner;
public class P1702 {
public staticvoid main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String mode, operate;
int num, a;
while (t-- > 0) {
int n = sc.nextInt();
mode = sc.next();
if (mode.charAt(2) == 'F') {
Queue queue = new Queue(n);
for (int i = 0; i < n; i++) {
operate = sc.next();
if (operate.charAt(0) == 'I') {
num = sc.nextInt();
queue.add(num);
} else {
a = queue.pop();
if (a == 0) {
System.out.println("None");
} else {
System.out.println(a);
}
}
}
} else {
Stacks stack = new Stacks(n);
for (int i = 0; i < n; i++) {
operate = sc.next();
if (operate.charAt(0) == 'I') {
num = sc.nextInt();
stack.inStack(num);
} else {
a = stack.outStack();
if (a == 0) {
System.out.println("None");
} else {
System.out.println(a);
}
}
}
}
}
}
}
class Queue {
int end;
finalint FRONT = 0;
int[] queue;
public Queue(int n) {
end = 0;
queue = newint[n+10];
}
publicvoid add(int p) {// 入队
queue[end++] = p;
}
publicint pop() {// 出队
if (end <= 0) {
return 0;
}
int p = queue[FRONT];
if (end > 1) {// 队首出队,则后面的要补上来
for (int i = 0; i < end; i++) {
queue[i] = queue[i + 1];
}
}
end--;
return p;
}
}
class Stacks {
int top;
int[] stack;
public Stacks(int n) {
top=0;
stack = newint
;
}
publicvoid inStack(int p) {
stack[top++] = p;
}
publicint outStack() {
if (top==0) {
return 0;
}
return stack[--top];
}
}
相关文章推荐
- 2015 Multi-University Training Contest-6 Key Set
- 2015 Multi-University Training Contest-5 MZL's chemistry
- 2015 Multi-University Training Contest-5 MZL's xor
- hdu1022(Train Problem I)----- 典型栈类题目
- failed to load the jni shared library jvm
- 多图细数TurboMail邮件系统丰富的用户权限管理
- 跨时代产品,TurboMail邮件系统V7.0
- 解决mvn install问题Failure to find ** was cached in the local repository
- UltraISO制作U盘启动盘安装Win7/9/10系统攻略
- HDOJ 1022 Train Problem I (Stack)
- icloud无法注销,icloud服务中keyChain钥匙串无法关闭问题的解决。
- Codeforces 148E Porcelain (预处理+多重背包)
- ZOJ 3725 - Painting Storages(DP)
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
- leetcode 217 Contains Duplicate 数组中是否有重复的数字
- hdu 1789 Doing Homework again 状态压缩DP
- 利用keychain保存iphone唯一识别码idfv
- 2015 HUAS Summer Trainning #4 D
- 2015 HUAS Summer Trainning #4 C
- whust #0.2 J Jailbreak (未AC)