Editorial
For subtask , enumerate every interval of and directly check whether it also appears in .
For the full problem, let be the maximum length of a common interval ending at both and . If the two elements match, extend the common interval ending at the preceding elements. Otherwise, no nonempty common interval ends at both positions. Thus
The answer length is the maximum of all . When this maximum increases, also store its ending position in . The corresponding elements ending at that position form an answer.
Only the previous DP row is needed, so each case takes time. The DP uses space, and the total space including the input sequences is .
Solution written by GPT6