Editorial for Yet Another Contest 1 P4 - No More Searching
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For subtask 1, we simply brute-force over all possible ways to break the string into subsequences.
Time complexity:
Subtask 2
For subtask 2, notice that all possible scores will be a multiple of . This is because each subsequence will either contribute a score of:
- , if the first and last characters are the same
- , if the first character is
A
and the last character isB
- or , if the first character is
B
and the last character isA
So, we can find the maximum number of subsequences with A
as the starting character and B
as the ending character by iterating through the string and keeping track of the number of A
s. We will always pair up a B
with an A
if there are any A
s available. We can then repeat this process again, iterating in reverse this time to find the maximum number of subsequences with B
as the starting character and A
as the ending character. Afterwards, we will have found the minimum and maximum possible total scores for the string. Finally, we notice that it is possible to achieve any total score between the minimum and maximum total score that is a multiple of , since we can just choose to remove the contribution of any sequence by making all of its characters pair with itself as separate subsequences instead.
Time complexity:
Subtask 3
For subtask 3, we should first make the observation that the expression for the score of a subsequence can be rewritten as . So, the contribution from starting or ending a subsequence with a character is independent from the other character at the beginning or end of the subsequence.
Now, we will use dynamic programming. Define if it is possible to have a total score of from the first characters such that there are "open" sequences, and otherwise. Note that the absolute value of the total score for a string of length is bounded by .
There are 3 cases to consider when transitioning from to :
- We start a new subsequence with as the first character.
- will contribute to the total score. So, we transition from .
- We end an "open" sequence with as the last character.
- will contribute to the total score. So, we transition from .
- We make a subsequence of length just containing , or we add to an "open" sequence without ending it.
- will not contribute to the total score. So, we transition from .
To save memory, notice that transitions to come directly from , allowing us to cut down one dimension in our DP array. We can also improve our constant factor by instead defining to be the set of possible total scores from the first characters such that there are "open" sequences, with similar transitions.
Time complexity:
Subtask 4
For subtask 4, we can optimize our previous DP transitions with bitsets. In Java or Python, it suffices to use arbitrarily large integers to represent bitsets. The implementation is left as an exercise to the reader.
Time complexity:
Comments
very educational nice problem, thanks, first time I used bitset, cool!
me after solving