1. 程式人生 > >hdu 6194 string string string —— 字尾陣列找出現k次的串

hdu 6194 string string string —— 字尾陣列找出現k次的串

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. 注意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;
}