题目描述
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之内了。
在判断过程中将所有符合条件的记录下来,最后输出即可。
代码如下
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#define ULL unsigned long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=1e5+5,P=131;
char s[N];
ULL p[N],h[N];
int main()
{
p[0]=1;
for(int i=1;i<N;i++) p[i]=p[i-1]*P;
int m,l;
while(~scanf("%d%d",&m,&l))
{
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++) h[i]=h[i-1]*P+s[i];
int ans=0;
for(int st=1;st<=l;st++) //枚举起点(st=l+1与st=1是重复的,因此只需要枚举到l)
{
queue<int> q; //用队列保证m个长度为l的子串
unordered_map<ULL,int> f; //记录每个子串的hashcode
for(int i=st;i+l-1<=n;i+=l) //枚举起点
{
ULL val=h[i+l-1]-h[i-1]*p[l]; //计算当前段的hashCode
q.push(i); //将当前位置放入队列
if(q.size()>m) //如果队列中元素大于m,则删除首元素
{
int t=q.front();
q.pop();
ULL x=h[t+l-1]-h[t-1]*p[l];
f[x]--;
if(f[x]==0) f.erase(x); //如果f[x]==0,那么直接删除该位置
}
f[val]++; //用f记录val
if(q.size()==m&&f.size()==m) ans++; //如果f.size()==m,说明f中没有重复的val,答案+1
}
}
printf("%d\n",ans); //输出答案
}
return 0;
}