重复子串

1.可重叠最长重复子串。

#include <cstdio>
#include <iostream>
#include <cstring>
#define  LL long long
using namespace std;
const int MAXN=200010;
int wa[MAXN],wb[MAXN],wv[MAXN],Ws[MAXN];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}

void da(const char r[],int sa[],int n,int m)//用倍增求后缀数组
{
      int i,j,p,*x=wa,*y=wb,*t;
      for(i=0; i<m; i++) Ws[i]=0;
      for(i=0; i<n; i++) Ws[x[i]=r[i]]++;//以字符的ascii码为下标
      for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
      for(i=n-1; i>=0; i--) sa[--Ws[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++) Ws[i]=0;
            for(i=0; i<n; i++) Ws[wv[i]]++;
            for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
            for(i=n-1; i>=0; i--) sa[--Ws[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++;
      }
      return;
}
int sa[MAXN],Rank[MAXN],height[MAXN];
//求height数组
void calheight(const char *r,int *sa,int n)
{
      int i,j,k=0;
      for(i=1; i<=n; i++) Rank[sa[i]]=i;
      for(i=0; i<n; height[Rank[i++]]=k)
      for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++);
      // Unified
      for(int i=n;i>=1;--i) ++sa[i],Rank[i]=Rank[i-1];
}

char c[MAXN];
int main()
{
    while(~scanf("%s",c))
    {
           int t=strlen(c);
            da(c,sa,t,128);
         calheight(c,sa,t);
         int ans=0;
         for(int i=0;i<t;i++)
         {
             if(ans<height[i])
                {
                    ans=height[i];
                }
         }
         printf("%d
",ans);
    }
    return 0;
}

2.不可重叠子串:

#include <cstdio>
#include <iostream>
#include <cstring>
#define  LL long long
using namespace std;
const int MAXN=20010;
const int maxn=20010;
int wa[MAXN],wb[MAXN],wv[MAXN],Ws[MAXN];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}

void da( char r[],int sa[],int n,int m)//用倍增求后缀数组
{
      int i,j,p,*x=wa,*y=wb,*t;
      for(i=0; i<m; i++) Ws[i]=0;
      for(i=0; i<n; i++) Ws[x[i]=r[i]]++;//以字符的ascii码为下标
      for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
      for(i=n-1; i>=0; i--) sa[--Ws[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++) Ws[i]=0;
            for(i=0; i<n; i++) Ws[wv[i]]++;
            for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
            for(i=n-1; i>=0; i--) sa[--Ws[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++;
      }
      return;
}
int sa[MAXN],Rank[MAXN],height[MAXN];
//求height数组
void calheight( char *r,int *sa,int n)
{
      int i,j,k=0;
      for(i=1; i<=n; i++) Rank[sa[i]]=i;
      for(i=0; i<n; height[Rank[i++]]=k)
      for(k?k--:0,j=sa[Rank[i]-1]; r[i+k]==r[j+k]; k++);
      // Unified
      for(int i=n;i>=1;--i) ++sa[i],Rank[i]=Rank[i-1];
}
int check(int n,int k){
    int maxx=sa[1],minx=sa[1];
    for(int i=2;i<n;++i){
        if(height[i]<k) maxx=minx=sa[i];
        else{
            if(sa[i]<minx) minx=sa[i];
            if(sa[i]>maxx) maxx=sa[i];
            if(maxx-minx>=k) return true;
        }//每个后缀sa的最大值和最小值只差是否不小于k;
    }
    return false;
}
char str[maxn];
int main()
{

    while(~scanf("%s",str))
    {
        getchar();
           int n=strlen(str);
      da(str,sa,n,176);
      calheight(str,sa,n-1);

       int l=0,r=n/2;
        int ans=0;
        while(l<=r){
            int mid=(l+r)>>1;
            if(check(n,mid)){
                l=mid+1;
                ans=max(mid,ans);
            }
            else
                r=mid-1;
        }//二分搜索存不存在

        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/2014slx/p/7906665.html