Given a string S and two integers L and M, we consider a substring of S as “recoverable” if and only if (i) It is of length M*L; (ii) It can be constructed by concatenating M “diversified” substrings of S, where each of these substrings has length L; two strings are considered as “diversified” if they don’t have the same character for every position. Two substrings of S are considered as “different” if they are cut from different part of S. For example, string “aa” has 3 different substrings “aa”, “a” and “a”. Your task is to calculate the number of different “recoverable” substrings of S.
Input
The input contains multiple test cases, proceeding to the End of File. The first line of each test case has two space-separated integers M and L. The second ine of each test case has a string S, which consists of only lowercase letters. The length of S is not larger than 10^5, and 1 ≤ M * L ≤ the length of S.
Output
For each test case, output the answer in a single line.
Sample Input 3 3 abcabcbcaabc Sample Output 2
给你一个字符串s。问最多能找出多少个符合要求的子串。 要求:1.该子串长度为m*l。2.将该子串分割为m个长度为l的子串,要求这m个子串互不相同。
这个题思路其实也不难。枚举s中所有长度为m*l的子串。 再将这个子串分割为m个长度为l的字符串,再通过map+字符串哈希判断这m个子串中有没有重复。
但这样做的时间复杂度过高,因此我们还需要进一步优化: 当枚举某一个位置的m*l字符串时,当判断完毕之后,我们可以先不向后枚举,而是删去开头的长度为l的子串,而在字符串末尾加上后面的一个长度为l的子串。这样就是一种新的情况,重复这个操作直到字符串后面剩的字符不到l个为止。 这样就可以把外圈循环的次数控制在l之内了。 在判断过程中将所有符合条件的记录下来,最后输出即可。
