Problem Description
Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle Mao was so lazy that he left the problem to you. I hope you can give him a solution.
Given a string s, we define a substring that happens exactly k times as an important string, and you need to find out how many substrings which are important strings.
Input
The first line contains an integer T (T≤100) implying the number of test cases.
For each test case, there are two lines:
the first line contains an integer k (k≥1) which is described above;
the second line contain a string s (length(s)≤105).
It’s guaranteed that ∑length(s)≤2∗106.
Output
For each test case, print the number of the important substrings in a line.
Sample Input
2
2
abcabc
3
abcabcabcabc
Sample Output
6
9
Source
2017 ACM/ICPC Asia Regional Shenyang Online
题意:
给你一个k和一个字符串,让你求在这个串中出现了k次的子串有多少子串(子串可区间重复)
题解:
出现k次的子串必然是在rank排名从i到i+k-1的地方找的,那么只需要找到i到i+k-1区间的最小值再减去两边的最大值就好了,但是从i开始的height是在i+1这个位置产生的,因为height[i]=rank(i)-rank(i-1),所以找的位置就变成了i+1,i+k-1.为什么返回的是最小值减去两边的最大值呢?举个例子,假设height[i+1]是abc,height[i]是ab,就代表在这个区间里ab不是出现k次的,所以最后只剩下abc就是1种可能,若height[i]是0,那么便是有a,ab,abc这3种
注意k=1的情况这样会出错,因为这种情况无法从i+1这个位置开始找,那么就是从i开始的后缀中有几个是只出现1次的了。所以是n-sa[i]-max(height[i],height[i+1])表示当前后缀长度减去当前排名和上一个排名的最长公共前缀和当前排名和下一个排名的最长公共前缀的最大值
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int MAXN =(int)1e5+10;
int wa[MAXN],wb[MAXN],wv[MAXN],we[MAXN],rk[MAXN];
int cmp(int *r,int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];}
void build_sa(int *r,int *sa,int n,int m){
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++)we[i]=0;
for(i=0;i<n;i++)we[x[i]=r[i]]++;
for(i=1;i<m;i++)we[i]+=we[i-1];
for(i=n-1;i>=0;i--)sa[--we[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++)y[p++]=i;
for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=0;i<n;i++)wv[i]=x[y[i]];
for(i=0;i<m;i++)we[i]=0;
for(i=0;i<n;i++)we[wv[i]]++;
for(i=1;i<m;i++)we[i]+=we[i-1];
for(i=n-1;i>=0;i--)sa[--we[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
int height[MAXN];
void calheight(int *r,int *sa,int n){
int i,j,k=0;
for(i=1;i<=n;i++)rk[sa[i]]=i;
for(i=0;i<n;height[rk[i++]]=k){
for(k?k--:0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
}
}
int sa[MAXN],a[MAXN],next[MAXN];
char s[MAXN];
int n,k;
int minn[MAXN*4];
void pushup(int root)
{
minn[root]=min(minn[root<<1],minn[root<<1|1]);
}
void build(int l,int r,int root)
{
if(l==r)
{
minn[root]=height[l];
return ;
}
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
pushup(root);
}
int query(int l,int r,int root,int ql,int qr)
{
if(l>=ql&&r<=qr)
return minn[root];
int mid=l+r>>1;
int ans=1e9;
if(mid>=ql)
ans=query(l,mid,root<<1,ql,qr);
if(mid<qr)
ans=min(ans,query(mid+1,r,root<<1|1,ql,qr));
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=0;i<=n;i++)
minn[i]=1e9;
scanf("%d",&k);
scanf("%s",s);
n=strlen(s);
for(int i=0;i<n;i++)
a[i]=s[i]-'a'+1;
a[n]=0;
build_sa(a,sa,n+1,100);
calheight(a,sa,n);
height[n+1]=0;
int ans=0;
build(1,n,1);
if(k==1)
{
for(int i=1;i<=n;i++)
ans+=max(n-sa[i]-max(height[i],height[i+1]),0);
}
else
{
for(int i=1;i<=n-k+1;i++)
ans+=max(query(1,n,1,i+1,i+k-1)-max(height[i],height[i+k]),0);
}
printf("%d\n",ans);
}
return 0;
}