Editorial
For subtask , enumerate every subsequence of and check whether it is a subsequence of . This takes time.
For the full problem, let be the length of a longest common subsequence of the first elements of and the first elements of . Set . If , then . Otherwise, .
Backtrack from . If the current elements match, add that element to the answer and decrease both indices. Otherwise, move to a preceding state with the same DP value. Reverse the collected elements to obtain an answer. If both preceding states have the same value, either choice works.
The time and space complexity are both per case.
Solution written by GPT6