Problem 728
Subsets II
Medium · 20 pts · Recursion, Backtracking
Problem
The array may contain duplicates. List every distinct subset of it, where two subsets are the same if they contain the same values with the same multiplicities. Write each subset in non-decreasing order, and list the subsets in lexicographic order (compare element by element; a subset that is a prefix of another comes first). Print the number of distinct subsets including the empty one, then every non-empty subset. Sort the array and skip a value when it equals the previous one at the same recursion depth.
Input
The first line contains N. The second line contains N integers.
Output
Print the number of distinct subsets (counting the empty subset) on the first line, then each non-empty subset on its own line in lexicographic order.
Constraints
1 <= N <= 10; -10 <= values <= 10
Example input
3 1 2 2
Example output
6 1 1 2 1 2 2 2 2 2
Explanation
For 1 2 2 the distinct subsets are {}, {1}, {1,2}, {1,2,2}, {2} and {2,2}, so there are 6. The five non-empty ones are listed in lexicographic order: 1, 1 2, 1 2 2, 2, 2 2.