hdu1702(ACboy needs your help again!) 在杭电又遇坑了

点击打开链接

结题感悟:

其实吧,这题并不是很难,就是一个栈和队列的公共题,也就是按指定的方式(栈或队列)存取数据,但是为什么我自己写的栈和队列就是不能再杭电ac(一直wa啊),而用java包中的栈和队列就秒过了,问题尚未找出原因,值得思考啊。不过可以趁此学学这两个类(尽量还是自己动手写的好啊)

栈:java.util 类 Stack<E>

Stack 类表示后进先出(LIFO)的对象堆栈。它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈。它提供了通常的push
pop 操作,以及取堆栈顶点的 peek 方法、测试堆栈是否为空的 empty 方法、在堆栈中查找项并确定到堆栈顶距

离的 search 方法。

构造方法:Stack()   创建一个空堆栈。
方法:
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 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

直接用java包中的类:

package stack;

import java.util.ArrayDeque;
import java.util.Scanner;
import java.util.Stack;

public class P1702_2 {

	public static void 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 static void 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;
	final int FRONT = 0;
	int[] queue;

	public Queue(int n) {
		end = 0;
		queue = new int[n+10];
	}

	public void add(int p) {// 入队
		queue[end++] = p;
	}

	public int 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 = new int[n];
	}

	public void inStack(int p) {
		stack[top++] = p;
	}

	public int outStack() {
		if (top==0) {
			return 0;
		}
		return stack[--top];
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 07:47:44

hdu1702(ACboy needs your help again!) 在杭电又遇坑了的相关文章

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

一个人的旅行 HDU杭电2066【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段时间,可不

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

杭电1276--士兵队列训练问题

士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4605    Accepted Submission(s): 2148 Problem Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行

杭电1272 并查集找环+判断连通

杭电1272 并查集找环+判断连通 E - E Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1272 Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B

杭电ACM Java实现样例

若使用Java求解杭电ACM,答案提交必须注意两点: 1.类名一定得是Main,否则服务器无法编译: 2.必须用while进行输入判断. 以1000题测试题为例,代码如下: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); while(scan.hasNextInt()) { int a=scan.n

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste