Блог пользователя TuongOnArrival1

Автор TuongOnArrival1, история, 2 месяца назад, По-английски

Prolem — URL : https://mirror.codeforces.com/contest/223/problem/B. Thanks for reading my stupid blog

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

»
2 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

You want every character in $$$s$$$ to be part of at least one subsequence, instead of trying to find every subsequence and then see if any are good, you can find intervals and if the position is in any interval then it is part of at least one subsequence.

How do we find such intervals ?

  1. Find the first subsequence possible in $$$s$$$, the positions of all of these characters that construct the first subsequence will be the first part of the interval
  2. Find the last subsequence possible in $$$s$$$, the positions of all of these characters that construct the last subsequence will be the second part of the interval

Let's look at an example : $$$s$$$ = abadbcd $$$t$$$ = abcd

After out first iteration we will have the values : a = [0, ] b = [1, ] c = [5, ] d = [6, ]

After out second iteration we will have the values : a = [, 2] b = [, 4] c = [, 5] d = [, 6]

Now we combine them and have the intervals : a = [0, 2] b = [1, 4] c = [5, 5] d = [6, 6]

Now we can iterate through all the letters in $$$s$$$ and check if they are in the interval.

The problem with this approach is that it only works for any $$$t$$$ that contains no duplicates.

For the input $$$s$$$ = ababa $$$t$$$ = abba this will fail because the 'a' at index 2 cannot be part of any subsequence but still appears in our interval.

To solve this problem we have to differentiate between the 'a' at position 0 in $$$t$$$ and the 'a' at position 3 in $$$t$$$. We do this by holding an array of intervals for each letter instead of a big interval for each one.

Interval for 'a': Before -> [0,4] After -> $$$[0,0]\cup[4,4]$$$, which now allows us to find the correct answer for all inputs.

After finding all intervals for each letter we can flatten them on a vector for each character in the alphabet.