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 ,
a sequence
is called a subsequence of
if and only if
can be obtained
by removing some elements (possibly none) from
.
The table below shows some examples of subsequences of a sequence .
Subsequence | How it can be obtained from |
---|---|
[3, 2, 1, 2] | No elements are removed. |
[2, 1, 2] | [ |
[3, 2, 2] | [3, 2, |
[3, 2] | [3, |
[3] | [3, |
[ ] | [ |
On the other hand, or
are not subsequences of
.
Consider two sequences of hieroglyphs, and
.
A sequence
is called a common subsequence of
and
if and only if
is a subsequence of both
and
.
Moreover, we say that a sequence
is a universal common subsequence of
and
if and only if the following two conditions are met:
is a common subsequence of
and
.
- Every common subsequence of
and
is also a subsequence of
.
It can be shown that any two sequences and
have at most one universal common subsequence.
The researchers have found two sequences of hieroglyphs and
.
Sequence
consists of
hieroglyphs
and sequence
consists of
hieroglyphs.
Help the researchers compute
a universal common subsequence of sequences
and
,
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)
: array of length
describing the first sequence.
: array of length
describing the second sequence.
- If there exists a universal common subsequence of
and
, the procedure should return an array containing this sequence. Otherwise, the procedure should return
(an array of length
, whose only element is
).
- This procedure is called exactly once for each test case.
Constraints
for each
such that
for each
such that
Subtasks
Subtask | Score | Additional Constraints |
---|---|---|
1 | ||
2 | For any integer |
|
3 | ||
4 | There exists a universal common subsequence of |
|
5 | ||
6 | 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 and
are the following:
,
,
,
,
,
,
,
,
,
,
,
,
and
.
Since is a common subsequence of
and
, and
all common subsequences of
and
are subsequences of
,
the procedure should return
.
Example 2
Consider the following call.
ucs([0, 0, 2], [1, 1])
Here, the only common subsequence of and
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 and
are
and
.
It can be shown that a universal common subsequence does not exist.
Therefore, the procedure should return
.
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, is the array returned by
ucs
and is its length.
Attachment Package
The sample grader along with sample test cases are available here.
Comments