#C9829. Generate Combinations Excluding Specific Elements

    ID: 53965 Type: Default 1000ms 256MiB

Generate Combinations Excluding Specific Elements

Generate Combinations Excluding Specific Elements

Given a list of elements, an integer k representing the combination length, and a list of excluded elements, your task is to generate all unique combinations with replacement of length k from the original list after removing any element that appears in the excluded set. In other words, you are to form all sequences \(a_1, a_2, \dots, a_k\) such that each \(a_i\) is drawn from the filtered list and \(a_1 \le a_2 \le \dots \le a_k\) (i.e. combinations with replacement).

If no valid combinations exist (for instance, when the filtered list is empty or k ≤ 0), output nothing.

inputFormat

The input is provided from standard input in the following format:

N
E1 E2 ... EN
k
M
X1 X2 ... XM

Where:

  • N is the number of elements.
  • E1, E2, ..., EN are the elements (each a string token; numeric tokens are also given as strings).
  • k is the integer length of the combination to generate.
  • M is the number of excluded elements.
  • X1, X2, ..., XM are the elements to be excluded.

Elements are separated by spaces, and each token is separated by whitespace or a newline.

outputFormat

The output is produced on standard output and consists of each valid combination printed on a new line. Each combination is a sequence of k tokens (separated by a single space) arranged in non-decreasing order. If there are no valid combinations, output nothing.

## sample
5
1 2 3 4 5
3
2
2 4
1 1 1

1 1 3 1 1 5 1 3 3 1 3 5 1 5 5 3 3 3 3 3 5 3 5 5 5 5 5

</p>