SubstringCoverage
SRM 853 · 2024-02-21 · by misof
Problem Statement
Given two strings S and T, an occurrence of S in T is a contiguous substring of T that exactly matches S.
The string T is covered by occurrences of S if each character of T is a part of one or more occurrences of S in T.
For example, the string T1 = "ababaaba" is covered by occurrences of S = "aba" but the string T2 = "abaaaba" isn't because the middle letter of T2 isn't in any occurrence of S in T2.
You are given the
The length X is called coverable if there is a
Given needle and an
Constraints
- needle will have between 1 and 5,000 characters, inclusive.
- Each character of needle will be a lowercase English letter ('a'-'z').
- H will be between 1 and 10^9, inclusive.
"abc" 11 Returns: 3
Out of all possible strings of all lengths up to 11, only three are covered by the substring "abc". These are the strings "abc", "abcabc", and "abcabcabc". Thus, there are three coverable lengths in the given range.
"abracadabra" 24 Returns: 4
"coco" 20 Returns: 9
"abracadabra" 10 Returns: 0
"abracadabra" 11 Returns: 1
Submissions are judged against all 97 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SubstringCoverage with a public method int count(string needle, int H) · 97 test cases · 2 s / 256 MB per case