Problem Description
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
题意: 将字符串分成长度为 m*l 的子串,比如样例中abcabcbcaabc
应该分成abcabcbca
、bcabcbcaa
、cabcbcaab
和abcbcaabc
。再将这些子串分成 m 个长度为 l 的小子串,比如abcabcbca
应该分成abc
、abc
和bca
;如果某个子串中所有的小子串没有重复,则总值加一,最后输出这个总值。
题解: 就是题目的理解上比较费劲,但本质上就是一道简单的字符串哈希的题目。值得注意的是,当字符串总长度足够长的时候,第 i 个子串中包含的小子串,和第 i+l 个子串中包含的小子串是有重合的,这个大家可以自己思考验证一下。所以可以从这个切入点去优化一下,减小时间复杂度。
因此我们希望在检查了第 i 个子串中小子串是否有重复之后,不要第一时间去检查第 i+1个子串,而应该是第 i+l 个子串。这样我们只需要将之前所记录,删除第一个小子串之后,在记录下一个小子串即可判重。实现方法是借助map,虽然set自带去重功能,但是你不知道这个小子串到底出现了几次,因此在删除操作上比较麻烦。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
typedef unsigned long long ull;
int m, l;
ull tem[100005] = {1};
char s[100005];
ull hs[100005];
int main(){
for (int i = 1; i < 100005; i++) tem[i] = tem[i - 1] * 131;
while(~scanf("%d%d", &m, &l)){
scanf("%s", s + 1);
int sum = 0;
int len = strlen(s + 1);
for (int i = 1; i < 100005; i++) hs[i] = 0;
for (int i = 1; i <= len; i++) hs[i] = hs[i - 1] * 131 + s[i] - 'a';
for (int i = 0; i < l; i++){
int flag = 0;
map<ull, int> mp;
for (int j = 1, k = 1; i + j * l <= len; j++){
ull a = hs[i + j * l] - hs[i + (j - 1) * l] * tem[l];
if(mp[a]==1) flag++;
mp[a]++;
if(j >= m){
if(!flag) sum++;
ull b = hs[i + k * l] - hs[i + (k - 1) * l] * tem[l];
k++, mp[b]--;
if(mp[b]==1) flag--;
}
}
}
printf("%d\n", sum);
}
return 0;
}