View problem - Hieroglyphs (IOI24_hieroglyphs)

Time limitMemory limit# of submissions# of submitted usersSolved #Accepted user ratio
1000 ms2048 MiB9516425.00%

A team of researchers is studying the similarities between sequences of hieroglyphs. They represent each hieroglyph with a non-negative integer. To perform their study, they use the following concepts about sequences.

For a fixed sequence AA, a sequence SS is called a subsequence of AA if and only if SS can be obtained by removing some elements (possibly none) from AA.

The table below shows some examples of subsequences of a sequence A=[3,2,1,2]A = [3, 2, 1, 2].

Subsequence How it can be obtained from AA
[3, 2, 1, 2] No elements are removed.
[2, 1, 2] [3, 2, 1, 2]
[3, 2, 2] [3, 2, 1, 2]
[3, 2] [3, 2, 1, 2] or [3, 2, 1, 2]
[3] [3, 2, 1, 2]
[ ] [3, 2, 1, 2]

On the other hand, [3,3][3, 3] or [1,3][1, 3] are not subsequences of AA.

Consider two sequences of hieroglyphs, AA and BB. A sequence SS is called a common subsequence of AA and BB if and only if SS is a subsequence of both AA and BB. Moreover, we say that a sequence UU is a universal common subsequence of AA and BB if and only if the following two conditions are met:

  • UU is a common subsequence of AA and BB.
  • Every common subsequence of AA and BB is also a subsequence of UU.

It can be shown that any two sequences AA and BB have at most one universal common subsequence.

The researchers have found two sequences of hieroglyphs AA and BB. Sequence AA consists of NN hieroglyphs and sequence BB consists of MM hieroglyphs. Help the researchers compute a universal common subsequence of sequences AA and BB, or determine that such a sequence does not exist.

Implementation details

You should implement the following procedure.

std::vector<int> ucs(std::vector<int> A, std::vector<int> B)
  • AA: array of length NN describing the first sequence.
  • BB: array of length MM describing the second sequence.
  • If there exists a universal common subsequence of AA and BB, the procedure should return an array containing this sequence. Otherwise, the procedure should return [1][-1] (an array of length 11, whose only element is 1-1).
  • This procedure is called exactly once for each test case.

Constraints

  • 1N100,0001 \leq N \leq 100,000
  • 1M100,0001 \leq M \leq 100,000
  • 0A[i]200,0000 \leq A[i] \leq 200,000 for each ii such that 0i<N0 \leq i < N
  • 0B[j]200,0000 \leq B[j] \leq 200,000 for each jj such that 0j<M0 \leq j < M

Subtasks

Subtask Score Additional Constraints
1 33 N=MN = M; each of AA and BB consists of NN distinct integers between 00 and N1N-1 (inclusive)
2 1515 For any integer kk, (the number of elements of AA equal to kk) plus (the number of elements of BB equal to kk) is at most 33.
3 1010 A[i]1A[i] \leq 1 for each ii such that 0i<N0 \leq i < N; B[j]1B[j] \leq 1 for each jj such that 0j<M0 \leq j < M
4 1616 There exists a universal common subsequence of AA and BB.
5 1414 N3000N \leq 3000; M3000M \leq 3000
6 4242 No additional constraints.

Examples

Example 1

Consider the following call.

ucs([0, 0, 1, 0, 1, 2], [2, 0, 1, 0, 2])

Here, the common subsequences of AA and BB are the following: [ ][\ ], [0][0], [1][1], [2][2], [0,0][0, 0], [0,1][0, 1], [0,2][0, 2], [1,0][1, 0], [1,2][1, 2], [0,0,2][0, 0, 2], [0,1,0][0, 1, 0], [0,1,2][0, 1, 2], [1,0,2][1, 0, 2] and [0,1,0,2][0, 1, 0, 2].

Since [0,1,0,2][0, 1, 0, 2] is a common subsequence of AA and BB, and all common subsequences of AA and BB are subsequences of [0,1,0,2][0, 1, 0, 2], the procedure should return [0,1,0,2][0, 1, 0, 2].

Example 2

Consider the following call.

ucs([0, 0, 2], [1, 1])

Here, the only common subsequence of AA and BB is the empty sequence [ ][\ ]. It follows that the procedure should return an empty array [ ][\ ].

Example 3

Consider the following call.

ucs([0, 1, 0], [1, 0, 1])

Here, the common subsequences of AA and BB are [ ],[0],[1],[0,1][\ ], [0], [1], [0, 1] and [1,0][1, 0]. It can be shown that a universal common subsequence does not exist. Therefore, the procedure should return [1][-1].

Sample Grader

Input format:

N  M
A[0]  A[1]  ...  A[N-1]
B[0]  B[1]  ...  B[M-1]

Output format:

T
R[0]  R[1]  ...  R[T-1]

Here, RR is the array returned by ucs and TT is its length.

Attachments
File nameSize
hieroglyphs.zip3.01 KiB