#P11052. [IOI 2024] 象形文字序列

[IOI 2024] 象形文字序列

Background

Please do not include hieroglyphs.h when submitting.

Do not submit in C++14 (GCC 9).

Problem Description

A research team is studying the similarity between hieroglyph sequences. They represent each hieroglyph as a non-negative integer. For their research, they use the following concepts about sequences.

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

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

Subsequence How to obtain the subsequence from AA
[3, 2, 1, 2] Remove no elements.
[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] is not a subsequence of AA.

Now consider two hieroglyph sequences 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. Furthermore, we say that a sequence UU is a universal common subsequence of AA and BB if and only if the following two conditions hold:

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

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

The researchers found two hieroglyph sequences AA and BB. Sequence AA contains NN hieroglyphs, and sequence BB contains MM hieroglyphs. Please help the researchers find a universal common subsequence of AA and BB, or determine that such a sequence does not exist.

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.

6 5
0 0 1 0 1 2
2 0 1 0 2

4
0 1 0 2

3 2
0 0 2
1 1

0


3 3
0 1 0
1 0 1

1
-1

Hint

Description

Implementation Details

You have to implement the following function.

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

Input Format

Constraints

  • 1N1000001 \leq N \leq 100\,000
  • 1M1000001 \leq M \leq 100\,000
  • For all ii with 0i<N0 \leq i < N, 0A[i]2000000 \leq A[i] \leq 200\,000
  • For all jj with 0j<M0 \leq j < M, 0B[j]2000000 \leq B[j] \leq 200\,000

Subtasks

Subtask Score Additional constraints
1 33 N=MN = M; both AA and BB consist of NN distinct integers, taken from 00 to N1N-1 (inclusive)
2 1515 For any integer kk, the total number of occurrences of kk in AA and BB is at most 33.
3 1010 For all ii with 0i<N0 \leq i < N, A[i]1A[i] \leq 1; for all jj with 0j<M0 \leq j < M, B[j]1B[j] \leq 1
4 1616 A universal common subsequence of AA and BB exists.
5 1414 N3000N \leq 3000; M3000M \leq 3000
6 4242 No additional constraints.

Examples

Example 1

Consider the following function call.

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

In this case, the common subsequences of AA and BB are: [ ][\ ], [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 function should return [0,1,0,2][0, 1, 0, 2].

Example 2

Consider the following function call.

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

In this case, the only common subsequence of AA and BB is the empty sequence [ ][\ ]. Therefore, the function should return an empty array [ ][\ ].

Example 3

Consider the following function call.

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

In this case, the common subsequences of AA and BB are [ ][\ ], [0][0], [1][1], [0,1][0, 1], and [1,0][1, 0]. We can see that a universal common subsequence does not exist. Therefore, the function should return [1][-1].

Output Format

Hint

Translated by ChatGPT 5