import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { static List<List<String>> res=new LinkedList<>(); public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); in.nextLine(); String[] str=in.nextLine().split(" "); LinkedList<String> track=new LinkedList<>(); HashMap<String, Integer> map=new HashMap<>(); backtrack(str,track,map); for (int i = 0; i <res.size() ; i++) { for (int j = 0; j <res.get(i).size() ; j++) { System.out.print(res.get(i).get(j)); if(j<res.get(i).size()-1){ System.out.print("-"); }else { System.out.print(" "); } } } System.out.println(res.size()); } private static void backtrack(String[] str, LinkedList<String> track,HashMap<String, Integer> map) { if(track.size()==str.length){ if(map.get("A")<map.get("B")){ res.add(new LinkedList<>(track)); return; } } for (int i = 0; i <str.length ; i++) { if(track.contains(str[i])){ continue; } track.add(str[i]); map.put(str[i],track.indexOf(str[i])); backtrack(str,track,map); map.remove(track.removeLast()); } } }
全部评论
(5) 回帖