1. 程式人生 > >字尾陣列(好)uva10829

字尾陣列(好)uva10829

L-Gap Substrings

If a string is in the form UVU, where Uis not empty, and V has exactly L characters, we say UVUis an L-Gap string. For example, abcbabc is a 1-Gapstring. xyxyxyxyxy is both a 2-Gap string and also a 6-Gapstring, but not a 10-Gap string (because U is non-empty).

Given a string s, and a positiveinteger g

, you are to find the number of g-Gap substrings in s.s contains lower-case letters only, and has at most 50,000characters.

Input

Thefirst line contains a single integer t(1<=t<=10), the number oftest cases. Each of the t followings contains an integer g(1<=g<=10)followed bya string s.

Output

For each test case, print thecase number and the number of g-Gapsubstrings. Look at the output for sample input for details.

Sample Input                               Output for Sample Input

2
1 bbaabaaaaa
5 abxxxxxab
Case 1: 7
Case 2: 1

題意:給出一個字串,找出滿足UVU形式的子串有多少個

思路:列舉U的長度,如bbaabaaaaa   l = 2時,分成    bb |  aa |  ba |  aa|  aa,那麼每個U肯定要包含劃分後的一個端點 ,然後向兩邊進行匹配,這個跟poj3693很像,兩邊匹配完之後假設匹配長度為len,那麼就有len-l個子串符合要求

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=50010;
typedef long long LL;
int sa1[maxn],height1[maxn],c[maxn],t[maxn],t2[maxn],sa2[maxn],height2[maxn];
char s[maxn],s1[maxn];
int g,d1[maxn][20],d2[maxn][20],rank1[maxn],rank2[maxn];
int n;
void build_sa(int *sa,int m,int n,char *s)
{
    int *x=t,*y=t2;
    for(int i=0;i<m;i++)c[i]=0;
    for(int i=0;i<n;i++)c[x[i]=s[i]]++;
    for(int i=1;i<m;i++)c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
    for(int k=1;k<=n;k<<=1)
    {
        int p=0;
        for(int i=n-k;i<n;i++)y[p++]=i;
        for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
        for(int i=0;i<m;i++)c[i]=0;
        for(int i=0;i<n;i++)c[x[y[i]]]++;
        for(int i=1;i<m;i++)c[i]+=c[i-1];
        for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];
        swap(x,y);p=1;
        x[sa[0]]=0;
        for(int i=1;i<n;i++)
            x[sa[i]]=(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k])?p-1:p++;
        if(p>=n)break;
        m=p;
    }
}
void getheight(int *sa,int n,int *rank,int *height,char *s)
{
    int k=0;
    for(int i=1;i<=n;i++)rank[sa[i]]=i;
    for(int i=0;i<n;i++)
    {
        if(k)k--;
        int j=sa[rank[i]-1];
        while(s[i+k]==s[j+k])k++;
        height[rank[i]]=k;
    }
}
void initRMQ(int n,int d[][20],int *height)
{
    for(int i=0;i<=n;i++)d[i][0]=height[i];
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<(j-1))<=n;i++)
        d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int LCP(int *rank,int d[][20],int a,int b)
{
    int x=rank[a],y=rank[b];
    if(x>y)swap(x,y);
    x++;
    int k=0;
    while((1<<(k+1))<=y-x+1)k++;
    return min(d[x][k],d[y-(1<<k)+1][k]);
}
void solve()
{
    LL ans=0;
    for(int l=1;l<=n;l++)
    {
        for(int i=0;i+l+g<n;i+=l)
        {
            int k=LCP(rank1,d1,i,i+l+g);
            k=min(l,k);
            int k1=LCP(rank2,d2,n-i-1,n-i-l-g-1);
            k1=min(k1,l);
            k=k+k1;
            if(k>=l)ans+=k-l;
        }
    }
    printf("%lld\n",ans);
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%s",&g,s);
        n=strlen(s);
        for(int i=0;i<=n;i++)s1[i]=s[n-1-i];
        build_sa(sa1,123,n+1,s);
        getheight(sa1,n,rank1,height1,s);
        initRMQ(n,d1,height1);

        build_sa(sa2,123,n+1,s1);
        getheight(sa2,n,rank2,height2,s1);
        initRMQ(n,d2,height2);
        printf("Case %d: ",cas++);
        solve();
    }
    return 0;
}