Lucid Strings
Time limit0.5sMemory limit512 MB
Count the substrings of a string of length n that split into k equal-length pieces where all k pieces are pairwise distinct.
- Level
Medium6 of 10
- Topics
- String matching, Hash map, String
- Solved
- No attempts yet
Problem
Consider a string over the 26 lowercase English letters. If the length of the string can be written as the product of two positive integers (≥ 2) and , the string can be split into substrings of the same length . If those substrings are pairwise distinct, the string is called a "-Lucid-String" (or "-LS" for short). A substring is a contiguous sequence of characters within a string.
For example, the string ababca has length 6, so it has three cases to check: . For , the string splits into aba and bca, which are distinct, so the string is a 2-LS. For , the string splits into three substrings of length 2, but ab appears twice, so it is not a 3-LS. For , the string splits into six substrings of length 1, but a and b each appear twice, so it is not a 6-LS.
Given a string of length and an integer , count the substrings of that are -LS. Two substrings count separately if they start at different positions in , even when their contents are equal.
For example, take ababca with . Each of the substrings ab, ba, ab, bc, and ca splits into two distinct substrings of length 1, so each is a 2-LS. The substrings babc and abca are also 2-LS, since each splits into two distinct substrings of length 2. The whole string is a 2-LS too, so ababca has 8 substrings that are 2-LS.
Input
The first line contains two integers (3 ≤ ≤ 40,000) and (2 ≤ ≤ 40,000). Here is the length of the string and is the number of equal-length substrings in each -LS. You can assume that ≤ . The second line contains a string of length .
Output
Print one line containing the number of substrings of the input string that are -LS.