HDU 6194 string string string【后缀数组】

来源:互联网 发布:知乎绑定合适的话题 编辑:程序博客网 时间:2024/06/05 06:58


string string string


TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 714    Accepted Submission(s): 206

Problem Description

UncleMao is a wonderful ACMER. One day he met an easy problem, but Uncle Mao was solazy 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 animportant string, and you need to find out how many substrings which areimportant strings.

 

 

Input

The first line contains an integer T (T≤100) implying thenumber of test cases.
For each test case, there are two lines:
the first line contains an integer
k (k≥1) which isdescribed 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

 


【题意】


给出一个字符串,问它的字串中有哪些在字符串中出现了k次。


【思路】


我们先处理出sa数组和height数组,然后由于sa数组相邻的两项是最接近的,也就是说出现了k次的子串它的起始位置在sa数组中是连续的。


那么我们考虑去枚举长度为区间,比如我们现在枚举的区间为[l,l+k-1],那么我们我们利用RMQ求出这一段区间的lcp(最长公共前缀)。


假设lcp为x,那么说明在枚举的区间内有x个子串在字符串中至少出现了k次,但是我们要求的是正好出现k次的。


很容易想到可以用至少出现k次的数目减去至少出现(k+1)次的便是正好出现k次的数目。


对于一个长度为k的区间来说,我们可以在头部或尾部加一个位置使它变成长度为(k+1)的区间(边界情况只有一种情况),那么对于可以在两端增加的,如果减去区间[l-1,l+k-1]和区间[l,l+k]的lcp,显然多减了区间[i-1,i+k]的lcp,再将这一部分加上即可。


#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn = 100005;const ll mod = 998244353;const int INF = 0x3f3f3f3f;const double eps = 1e-9;int t1[maxn],t2[maxn],c[maxn];bool cmp(int *r,int a,int b,int l){    return r[a]==r[b]&&r[a+l]==r[b+l];}void da(int *str,int *sa,int *Rank,int *height,int n,int m){    n++;    int i,j,p,*x=t1,*y=t2;    for(i=0; i<m; i++) c[i]=0;    for(i=0; i<n; i++) c[x[i]=str[i]]++;    for(i=1; i<m; i++) c[i]+=c[i-1];    for(i=n-1; i>=0; i--) sa[--c[x[i]]]=i;    for(j=1; j<=n; j<<=1)    {        p=0;        for(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<m; i++)  c[i]=0;        for(i=0; i<n; i++)  c[x[y[i]]]++;        for(i=1; i<m; i++)  c[i]+=c[i-1];        for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1,x[sa[0]]=0;        for(i=1; i<n; i++)        {            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;        }        if(p>=n) break;        m=p;    }    int k=0;    n--;    for(i=0; i<=n; i++) Rank[sa[i]]=i;    for(i=0; i<n; i++)    {        if(k) k--;        j=sa[Rank[i]-1];        while(str[i+k]==str[j+k]) k++;        height[Rank[i]]=k;    }}int Rank[maxn],height[maxn],sa[maxn];char str[maxn];int r[maxn];int dp[maxn][30];void initRMQ(int n){    for(int i=0;i<n;i++) dp[i][0]=height[i];    for(int j=1;(1<<j)<=n;j++)    for(int i=0;i+(1<<j)-1<n;i++)    {        dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);    }}int len;int lcp(int l,int r){    if(l==r)  return len-sa[r];    /*l=Rank[l],r=Rank[r];    if(l>r) swap(l,r);*/    l++;    int k=0;    while((1<<(k+1))<=r-l+1) k++;    return min(dp[l][k],dp[r-(1<<k)+1][k]);}int main(){    int k;    rush()    {        scanf("%d%s",&k,str);        int n=strlen(str);        len=n;        for(int i=0; i<n; i++)        {            r[i]=str[i]-'a'+1;        }        r[n]=0;        da(r,sa,Rank,height,n,30);           //得到sa,Rank,height数组        initRMQ(n+1);                        //RMQ预处理区间最长公共前缀        ll ans=0;        for(int i=1; i+k-1<=n; i++)          //枚举区间        {            ans+=lcp(i,i+k-1);            if(i>1) ans-=lcp(i-1,i+k-1);            if(i+k<=n) ans-=lcp(i,i+k);            if(i>1&&i+k<=n) ans+=lcp(i-1,i+k);        }        printf("%I64d\n",ans);    }    return 0;}




原创粉丝点击