Problem
Given a string s, and a positive integer g, you are to find the number of g-Gap substrings in s. s contains lower-case letters only, and has at most 50,000 characters.
Limits
TimeLimit(ms):3000
MemoryLimit(MB):NoLimit
|s|∈[1,5×104]
g∈[1,10]
字符集∈[a,z]
Look up Original Problem From here
Solution
调和级数方法搞…
Complexity
TimeComplexity:O(|s|×log2|s|)
MemoryComplexity:O(|s|)
My Code
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN 200100
#define N
#define M 20
int wa[MAXN],wb[MAXN],wv[MAXN],wss[MAXN],sa[MAXN],r[MAXN];
inline int cmp(int *r,int a,int b,int l){
return r[a]==r[b] && r[a+l]==r[b+l];
}
inline void build_sa(int *r,int *sa,int n,int m){
int i,j,p,*x=wa,*y=wb,*t;
rep(i,0,m) wss[i]=0;
rep(i,0,n) wss[x[i]=r[i]]+=1;
rep(i,1,m) wss[i]+=wss[i-1];
depin(i,n-1,0) sa[--wss[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;
rep(i,0,n) if(sa[i]>=j) y[p++]=sa[i]-j;
rep(i,0,n) wv[i]=x[y[i]];
rep(i,0,m) wss[i]=0;
rep(i,0,n) wss[wv[i]]+=1;
rep(i,1,m) wss[i]+=wss[i-1];
depin(i,n-1,0) sa[--wss[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 ranking[MAXN],height[MAXN];
inline void build_height(int *r,int *sa,int n){
int i,j,k=0;
repin(i,1,n) ranking[sa[i]]=i;
for(i=0;i<n;height[ranking[i++]]=k){
for(k?k--:0,j=sa[ranking[i]-1];r[i+k]==r[j+k];k++);
}
}
int logx[MAXN];
inline void Init_logx(){
for(int i=0;1<<i<MAXN;i++){
logx[1<<i]=i;
}
rep(i,1,MAXN){
if(!logx[i]) logx[i]=logx[i-1];
}
}
int dp[MAXN][M];
inline void RMQ_Init(int n){
repin(i,2,n){
dp[i][0]=height[i];
}
for(int j=1;1<<j<=n;j+=1){
for(int i=2;i+j-1<=n;i++){
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
}
inline int RMQ_Query(int l,int r){
int a=ranking[l],b=ranking[r];
if(a>b) swap(a,b);
a+=1;
int k=logx[b-a+1];
return min(dp[a][k],dp[b-(1<<k)+1][k]);
}
char s[MAXN];
int midl,lens,g;
ll ans;
int main(){
Init_logx();
int T;
scanf("%d",&T);
repin(Case,1,T){
scanf("%d %s",&g,s);
midl=lens=len(s);
s[lens++]='#';
depin(i,midl-1,0){
s[lens++]=s[i];
}
s[lens]='\0';
repin(i,0,lens){
r[i]=static_cast<int>(s[i]);
}
build_sa(r,sa,lens+1,'z'+1);
build_height(r,sa,lens);
RMQ_Init(lens);
ans=0;
for(int l=1;l<=midl;l++){
for(int i=0;i<midl;i+=l){
int p1=i,p2=i+l+g;
if(p2>=midl || s[p1]!=s[p2]) continue;
int p3=lens-1-p2;
int p4=lens-1-p1;
int leftcom=RMQ_Query(p3,p4);
int rightcom=RMQ_Query(p1,p2);
int com=leftcom+rightcom-1;
if(com<l) continue;
ans+=(ll)(com-l+1);
int p6=(p2-leftcom+1)+com;
int p5=p6-(g+l);
p5=(p5/l)*l;
i=p5;
}
}
printf("Case %d: %lld\n",Case,ans);
}
}