Pareidolia
Time limit4sMemory limit1024 MB
Given a lowercase string of up to 300000 characters, sum B(s) over all substrings s, where B(s) is how many copies of "bessie" can be picked as subsequences of s.
- Level
Medium7 of 10
- Topics
- Dynamic programming, String matching, Two pointers
- Solved
- No attempts yet
Problem
Pareidolia is the phenomenon where your eyes tend to see familiar patterns in images where none really exist, for example seeing a face in a cloud. Farmer John is constantly near cows, so he often sees cow-related patterns in everyday objects. For example, if he looks at the string "bqessiyexbesszieb", his eyes ignore some of the letters and all he sees is "bessiebessie".
Given a string , let be the maximum number of repeated copies of "bessie" that can be formed by deleting zero or more characters from . In the example above, .
Farmer John wants to solve an even more interesting challenge. Given a string of length at most consisting only of lowercase letters a-z, compute the sum of over all contiguous substrings of .
Input
The input consists of a nonempty string of length at most whose characters are all lowercase English letters.
Output
Output a single number, the total number of bessies that can be made across all substrings of the input string.