其实吧,这题并不是很难,就是一个栈和队列的公共题,也就是按指定的方式(栈或队列)存取数据,但是为什么我自己写的栈和队列就是不能再杭电ac(一直wa啊),而用java包中的栈和队列就秒过了,问题尚未找出原因,值得思考啊。不过可以趁此学学这两个类(尽量还是自己动手写的好啊)
Stack 类表示后进先出(LIFO)的对象堆栈。它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈。它提供了通常的push 和
pop 操作,以及取堆栈顶点的 peek 方法、测试堆栈是否为空的 empty 方法、在堆栈中查找项并确定到堆栈顶距
离的 search 方法。
Stack() 创建一个空堆栈。
boolean |
empty()
测试堆栈是否为空。 |
E |
peek()
查看堆栈顶部的对象,但不从堆栈中移除它。 |
E |
pop()
移除堆栈顶部的对象,并作为此函数的值返回该对象。 |
E |
push(E item)把项压入堆栈顶部。 |
int |
search(Object o)返回对象在堆栈中的位置,以 1 为基数。 |
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。 |
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
1 2 2 1 1 2 None 2 3
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());
}
}
}
}
}
}
}
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(ACboy needs your help again!) 在杭电又遇坑了
原文:http://blog.csdn.net/u011479875/article/details/47385933