I am stucked at the problem Shortest subsequence. Can someone kindly explain an optimal algorithm to solve this problem?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
I am stucked at the problem Shortest subsequence. Can someone kindly explain an optimal algorithm to solve this problem?
Название |
---|
Suppose we partition the string into $$$k$$$ contiguous subsegments such that the letters
GCAT
all appear at least once each in each partition. Then, it is clear that all $$$k$$$-character strings appear as subsequences.We can construct such a partition greedily. Find the shortest prefix of the string that contains all characters
GCAT
, make that one subsegment, then recurse on the remaining string. Note that this might actually partition it into $$$k+1$$$ subsegments, where the last subsegment is ``incomplete''. The last character in each subsegment (besides the incomplete subsegment) also appears exactly once in that subsegment; greedily, if it appeared earlier in the subsegment, then we could have ended this partition earlier.If $$$k$$$ is maximal, then we can show that there exists a $$$k+1$$$ length string that is not a subsequence. How? We can explicitly construct it as the last character in each of the partitions, plus some character not in the incomplete subsegment (or any character, if there is no incomplete subsegment).
Thanks a lot.
Shisuko Great explanation!
I have a question. Is it possible to count the number of such subsequences in a reasonable time limit? If possible then what is the approach?
we can use dp, refer the question below.
Very nice explanation dude
Can you explain why this is the optimal way?
All strings of length at most $$$k$$$ appear as a subsequence, so the answer has to be at least $$$k+1$$$.
But, we can actually do $$$k+1$$$. Thus, the minimum is exactly $$$k+1$$$.
Ohh!! We have greedily selected the minimum possible. thanks :)
atcoder similar question with editorial