Under construction

Still being built. Things may change

Hieroglyphs

Time limit1sMemory limit1024 MB

Summary
Find the universal common subsequence of two integer sequences A and B, or report that none exists.
Level

Hard8 of 10

Topics
Dynamic programming, Greedy, Array
Solved
No attempts yet

Problem

A team of researchers is studying the similarities between sequences of hieroglyphs. Each hieroglyph is represented by a non-negative integer.

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

SubsequenceHow it is obtained from AA
\[3,2,1,2]\[3, 2, 1, 2]No elements are removed.
\[2,1,2]\[2, 1, 2]\[\enclosehorizontalstrike3,2,1,2]\[\enclose{horizontalstrike}{3}, 2, 1, 2]
\[3,2,2]\[3, 2, 2]\[3,2,\enclosehorizontalstrike1,2]\[3, 2, \enclose{horizontalstrike}{1}, 2]
\[3,2]\[3, 2]\[3,\enclosehorizontalstrike2,\enclosehorizontalstrike1,2]\[3, \enclose{horizontalstrike}{2}, \enclose{horizontalstrike}{1}, 2] or \[3,2,\enclosehorizontalstrike1,\enclosehorizontalstrike2]\[3, 2, \enclose{horizontalstrike}{1}, \enclose{horizontalstrike}{2}]
\[3]\[3]\[3,\enclosehorizontalstrike2,\enclosehorizontalstrike1,\enclosehorizontalstrike2]\[3, \enclose{horizontalstrike}{2}, \enclose{horizontalstrike}{1}, \enclose{horizontalstrike}{2}]
\[]\[ ]\[\enclosehorizontalstrike3,\enclosehorizontalstrike2,\enclosehorizontalstrike1,\enclosehorizontalstrike2]\[\enclose{horizontalstrike}{3}, \enclose{horizontalstrike}{2}, \enclose{horizontalstrike}{1}, \enclose{horizontalstrike}{2}]

For example, \[3,3]\[3, 3] and \[1,3]\[1, 3] are not subsequences of A=\[3,2,1,2]A = \[3, 2, 1, 2].

For two sequences AA and BB, a sequence SS is a common subsequence of AA and BB if SS is a subsequence of both. A sequence UU is a universal common subsequence of AA and BB if both of the following hold:

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

Any two sequences AA and BB have at most one universal common subsequence.

The researchers have two sequences AA and BB. Sequence AA has NN hieroglyphs and sequence BB has MM hieroglyphs. Compute a universal common subsequence of AA and BB, or determine that none exists.

Constraints

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

Examples1

  1. Example 1

    Input
    1
    0
    1
    0
    
    Expected output
    0