Editorial for LCC '24 Contest 1 J5 - Counting Candy
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For this subtask, we can brute force all the possible subsequences of and check if they're equal to .
The only thing to be careful about is when is a palindrome. This can only occur when . Note that when is a palindrome, then the strings found going left to right and right to left are identical, so we only need to check in one direction.
Time Complexity: .
Subtask 2
This solution demonstrates searching for valid solutions searching from left to right. Searching from right to left can be implemented similarly.
For the rest of this solution, let denote the th character of a string . Let the string of candies he lays on the ground be .
We can consider the fact that the will always appear after in
This means that if we create an integer array of size , we can store the number of subsequences in the string equivalent to .
We do this by traversing backwards through the string . If and is the last character of , then we add 1 to . If it is not the last character of , then we add to , taking mod once added.
The answer is simply the first element of the array.
Our solution works by recording the total number of subsequences of starting from the th character as the th element of the array, starting from the th character of . As we traverse the string in reverse order, we're recording the number of subsequences of equal to each substring of that includes the last character, noting that any can be appended to the substring as long as , which is why we traverse through backwards.
Time Complexity: .
Comments