PAT 甲级 1051 JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static Set<List<Integer>> set = new HashSet<>();
    static int M;

    public static void main (String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] info1 = br.readLine().split(" ");
        M = Integer.parseInt(info1[0]);
        int N = Integer.parseInt(info1[1]);
        int N_lines = Integer.parseInt(info1[2]);
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= N; i++) {
            list.add(i);
        }
        dfs(list, 0, new ArrayList<>(), new ArrayList<>());
        /*
        for (List<Integer> l : set) {
            for (int i : l) {
                System.out.print(i + " ");
            }
            System.out.println();
        }

         */
        List<List<Integer>> big_l = new ArrayList<>();
        for (int i = 0; i < N_lines; i++) {
            String[] line = br.readLine().split(" ");
            List<Integer> l = new ArrayList<>();
            for (int j = 0; j < line.length; j++) {
                l.add(Integer.parseInt(line[j]));
            }
            big_l.add(new ArrayList<>(l));
        }
        for (List<Integer> l : big_l) {
            if (set.contains(l)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }

    }

    private static void dfs (List<Integer> list, int start, List<Integer> cur, List<Integer> poped) {
        if (poped.size() == list.size()) {
            set.add(new ArrayList<>(poped));
            return;
        }
        if (start == list.size()) {
            if (cur.size() != 0) {
                int temp = cur.remove(cur.size() - 1);
                poped.add(temp);
                dfs(list, start, cur, poped);
                cur.add(temp);
                poped.remove(poped.size() - 1);
            }
            return;
        }

        if (cur.size() == 0) {
            cur.add(list.get(start));
            dfs (list, start + 1, cur, poped);
            cur.remove(cur.size() - 1);
            return;
        }
        if (cur.size() == M) {
            int rem = cur.remove(cur.size() - 1);
            poped.add(rem);
            dfs(list, start, cur, poped);
            cur.add(rem);
            poped.remove(poped.size() - 1);
            return;
        }
        cur.add(list.get(start));
        dfs(list,start + 1, cur, poped);
        cur.remove(cur.size() - 1);
        int temp = cur.remove(cur.size() - 1);
        poped.add(temp);
        dfs(list, start, cur, poped);
        cur.add(temp);
        poped.remove(poped.size() - 1);

    }
}

有一个超时

主要思路是求出所有的可能的排序结果,放在set里,看输入的值是不是在set里


更正解法
按照他的每种步骤去模拟压入压出,如果与实际情况不符合,就返回NO

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static int M;

    public static void main (String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] info1 = br.readLine().split(" ");
        M = Integer.parseInt(info1[0]);
        int N = Integer.parseInt(info1[1]);
        int N_lines = Integer.parseInt(info1[2]);
        int[] arr = new int[N];
        for (int i = 1; i <= N; i++) {
            arr[i - 1] = i;
        }
        int[][] test = new int[N_lines][N];
        for (int i = 0; i < N_lines; i++) {
            String[] line = br.readLine().split(" ");
            for (int j = 0; j < N; j++) {
                test[i][j] = Integer.parseInt(line[j]);
            }
        }
        for (int i = 0; i < N_lines; i++) {
            if (dfs(arr, test, i)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }

    private static boolean dfs (int[] arr, int[][] test, int line) {
        int cnt_test = 0;
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < arr.length; i++) {
            stack.push(i + 1);
            while (!stack.isEmpty() && stack.peek() == test[line][cnt_test]) {
                stack.pop();
                cnt_test++;
            }
            if (stack.size() == M) return false;
        }
        return stack.isEmpty();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值