PivotOJ

Hieroglyphs

시간 제한: 1000ms메모리 제한: 1024MB출처: IOI 2024BOJ 32267

문제

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][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}]

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.

코드를 제출하려면 로그인하세요.