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];
	}

}








你可能感兴趣的:(队列,栈,hdu1702)