Java20分求助

P4387 【深基15.习9】验证栈序列

AUST_Ake @ 2024-02-18 15:53:32

讨论区的例子都能过,但是提交只能过第一个

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.LinkedList;

public class Main {
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in),32768));
    public static int nextInt() throws IOException { in.nextToken(); return (int)in.nval; }
    public static void main(String[] args) throws IOException {
        int q = nextInt();
        int ans[] = new int[q];
//        int index=0;
        while (q--!=0){
            int n = nextInt();
            LinkedList<Integer> queue1 = new LinkedList<>();
            LinkedList<Integer> queue2 = new LinkedList<>();
            for (int i = 0; i < n; i++) {
                queue1.offer(nextInt());
            }
            for (int i = 0; i < n; i++) {
                queue2.offer(nextInt());
            }

            LinkedList<Integer> stack = new LinkedList<>();

                for (int i = 0; i < n; i++) {
                    Integer q1 = queue1.poll();
                    stack.push(q1);
                    while(stack.size()!=0&&queue2.size()!=0&&stack.peek()==queue2.getFirst()){
                        stack.pop();
                        queue2.poll();
                    }
                }
                if (stack.size()==0){
                    System.out.println("Yes");
                }else {
//                    index++;
                    System.out.println("No");
                }

        }
//        for (int i = 0; i < index; i++) {
//            if (ans[i]==1){
//                System.out.println("Yes");
//            }else{
//                System.out.println("No");
//            }
//        }
    }
}
//1
//5
//1 2 3 4 5
//5 4 3 2 1
//1
//4
//1 2 3 4
//2 4 1 3

|