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();
}
}