#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 , a sequence is called a subsequence of if and only if can be obtained by removing some (possibly zero) elements from .
The table below shows some examples of subsequences of the sequence .
| Subsequence | How to obtain the subsequence from |
|---|---|
| [3, 2, 1, 2] | Remove no elements. |
| [2, 1, 2] | [ |
| [3, 2, 2] | [3, 2, |
| [3, 2] | [3, |
| [3] | [3, |
| [ ] | [ |
On the other hand, or is not a subsequence of .
Now consider two hieroglyph sequences and . A sequence is called a common subsequence of and if and only if is a subsequence of both and . Furthermore, we say that a sequence is a universal common subsequence of and if and only if the following two conditions hold:
- is a common subsequence of and .
- Every common subsequence of and is a subsequence of .
It can be proven that any two sequences and have at most one universal common subsequence.
The researchers found two hieroglyph sequences and . Sequence contains hieroglyphs, and sequence contains hieroglyphs. Please help the researchers find a universal common subsequence of and , 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, is the array returned by ucs, and 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)
- : an array of length , giving the first sequence.
- : an array of length , giving the second sequence.
- If and have a universal common subsequence, the function should return an array containing that sequence. Otherwise, the function should return (an array of length whose only element is ).
- For each test case, this function is called exactly once.
Input Format
Constraints
- For all with ,
- For all with ,
Subtasks
| Subtask | Score | Additional constraints |
|---|---|---|
| 1 | ; both and consist of distinct integers, taken from to (inclusive) | |
| 2 | For any integer , the total number of occurrences of in and is at most . | |
| 3 | For all with , ; for all with , | |
| 4 | A universal common subsequence of and exists. | |
| 5 | ; | |
| 6 | 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 and are: , , , , , , , , , , , , , and .
Since is a common subsequence of and , and all common subsequences of and are subsequences of , the function should return .
Example 2
Consider the following function call.
ucs([0, 0, 2], [1, 1])
In this case, the only common subsequence of and 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 and are , , , , and . We can see that a universal common subsequence does not exist. Therefore, the function should return .
Output Format
Hint
Translated by ChatGPT 5