A - Twice Subsequence 解説 by evima
If there is no subsequence of \(A\) that matches \(B\), the answer is No
. From here on, assume there is at least one such subsequence.
Find the subsequence of \(A\) that matches \(B\) and whose positions (the indices of the elements in \(A\) used to form the subsequence) are lexicographically smallest, and also find the one whose positions are lexicographically largest. If and only if these two sequences of positions differ, there exist at least two subsequences of \(A\) that match \(B\).
The subsequences with lexicographically smallest and largest positions can each be found in \(\mathrm{O}(N)\) time by a greedy approach scanning \(A\) from the front or from the back, respectively.
投稿日時:
最終更新: