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

點擊打開鏈接

結題感悟:

其實吧,這題並不是很難,就是一個棧和隊列的公共題,也就是按指定的方式(棧或隊列)存取數據,但是爲什麼我自己寫的棧和隊列就是不能再杭電ac(一直wa啊),而用java包中的棧和隊列就秒過了,問題尚未找出原因,值得思考啊。不過可以趁此學學這兩個類(儘量還是自己動手寫的好啊)

棧:java.util 類 Stack<E>

Stack 類表示後進先出(LIFO)的對象堆棧。它通過五個操作對類 Vector 進行了擴展 ,允許將向量視爲堆棧。它提供了通常的pushpop 操作,以及取堆棧頂點的 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];
	}

}








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章