import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int[] array = Arrays.stream(s.substring(1, s.length() - 1).split(", "))
.mapToInt(Integer::parseInt).toArray();
int n = Integer.parseInt(sc.nextLine());
System.out.println(get(array, n));
}
private static String get(int[] array, int n) {
ArrayList<Integer> link1 = new ArrayList<>();
ArrayList<Integer> link2 = new ArrayList<>();
Arrays.sort(array);
for (int i : array) {
if (i < 4) link1.add(i);
else link2.add(i);
}
ArrayList<LinkedList<Integer>> res = new ArrayList<>();
int c1 = link1.size();
int c2 = link2.size();
if (n == 1){
if (c1 == 1 || c2 == 1){
if (c1 == 1) dfs(link1, 0, 1, new LinkedList<>(), res);
if (c2 == 1) dfs(link2, 0, 1, new LinkedList<>(), res);
}else if (c1 == 3 || c2 == 3){
if (c1 == 3) dfs(link1, 0, 1, new LinkedList<>(), res);
if (c2 == 3) dfs(link2, 0, 1, new LinkedList<>(), res);
}else if (c1 == 2 || c2 == 2){
if (c1 == 2) dfs(link1, 0, 1, new LinkedList<>(), res);
if (c2 == 2) dfs(link2, 0, 1, new LinkedList<>(), res);
}else if (c1 == 4 || c2 == 4){
if (c1 == 4) dfs(link1, 0, 1, new LinkedList<>(), res);
if (c2 == 4) dfs(link2, 0, 1, new LinkedList<>(), res);
}
}else if (n == 2){
if (c1 == 2 || c2 == 2){
if (c1 == 2) dfs(link1, 0, 2, new LinkedList<>(), res);
if (c2 == 2) dfs(link2, 0, 2, new LinkedList<>(), res);
}else if (c1 == 4 || c2 == 4){
if (c1 == 4) dfs(link1, 0, 2, new LinkedList<>(), res);
if (c2 == 4) dfs(link2, 0, 2, new LinkedList<>(), res);
}else if (c1 == 3 || c2 == 3){
if (c1 == 3) dfs(link1, 0, 2, new LinkedList<>(), res);
if (c2 == 3) dfs(link2, 0, 2, new LinkedList<>(), res);
}
}else if (n == 4){
if (c1 == 4) res.add(new LinkedList<>(link1));
if (c2 == 4) res.add(new LinkedList<>(link2));
}else {
if (c1 == 4 && c2 == 4){
LinkedList<Integer> list = Stream.concat(link1.stream(), link2.stream())
.collect(Collectors.toCollection(LinkedList::new));
res.add(list);
}
}
return res.toString();
}
private static void dfs(ArrayList<Integer> link, int index, int c,
LinkedList<Integer> path, ArrayList<LinkedList<Integer>> res) {
if (path.size() == c){
res.add(new LinkedList<>(path));
return;
}
for (int i = index; i < link.size(); i++) {
path.add(link.get(i));
dfs(link, i + 1, c, path, res);
path.removeLast();
}
}
}
OJ_01
最新推荐文章于 2024-08-16 23:08:28 发布