DOJ logoDOJ
Problems
Recently added problemsSaved problems
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-backed certification.

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

Infinite String

Back to problem

Editorial

Let CCC be the set of characters that occur at least once in TTT.

Every character of a common subsequence must occur in T∞T^\inftyT∞, so it must belong to CCC. Thus, the answer cannot exceed the number of characters of SSS that belong to CCC.

Conversely, take every character of SSS that belongs to CCC, preserving its order, and call the resulting string XXX. Each character of XXX occurs somewhere in TTT. After any already selected position of T∞T^\inftyT, the next copy of contains the desired character, so all characters of can be selected one by one. Therefore, is a subsequence of .

Hence, the answer is exactly the number of characters of SSS that occur at least once in TTT.

The time complexity is O(∣S∣+∣T∣)O(|S|+|T|)O(∣S∣+∣T∣) and the extra space complexity is O(1)O(1)O(1).

Solution written by GPT5.6

∞
TTT
XXX
XXX
T∞T^\inftyT∞