DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

lcstring

Back to problem

Editorial

For subtask 111, enumerate every interval of AAA and directly check whether it also appears in BBB.

For the full problem, let Di,jD_{i,j}Di,j​ be the maximum length of a common interval ending at both AiA_iAi​ and BjB_jBj​. If the two elements match, extend the common interval ending at the preceding elements. Otherwise, no nonempty common interval ends at both positions. Thus

Di,j={Di−1,j−1+1,Ai=Bj,0,Ai≠Bj.D_{i,j}=\begin{cases}D_{i-1,j-1}+1,&A_i=B_j,\\0,&A_i\ne B_j.\end{cases}Di,j​={

The answer length is the maximum of all Di,jD_{i,j}Di,j​. When this maximum increases, also store its ending position in AAA. The corresponding KKK elements ending at that position form an answer.

Only the previous DP row is needed, so each case takes O(NM)O(NM)O(NM) time. The DP uses O(M)O(M)O(M) space, and the total space including the input sequences is O(N+M)O(N+M)O(N+M).

Solution written by GPT6

Di−1,j−1​+1,0,​Ai​=Bj​,Ai​=Bj​.​