后缀自动机DC3模板

后缀数组一些性质

  • suffix(j)和suffix(k)的最长公共前缀为height【rank【j】+1】到height【rank【k】】中的最小值;

  • 待更新

/*
POJ-2406
*/
#include
#include
#include
#include
using namespace std;
#define REP(i,n) for(i=0;i<(n);++i)
#define UPTO(i,l,h) for(i=(l);i<=(h);++i)
#define DOWN(i,h,l) for(i=(h);i>=(l);--i)
const int maxn=1e6+10;
const int mod=1e9+7;
typedef long long ll;
template <typename T, int LEN>
struct suffixarray{
    //rank[i] 第i个后缀的排名; SA[i] 排名为i的后缀位置; Height[i] 排名为i的后缀与排名为(i-1)的后缀的LCP
	int str[LEN*3],sa[LEN*3];
	int rank[LEN],height[LEN];
	int id[LEN];
	int best[LEN][20];
	int len;
	bool equal(int *str, int a, int b){
		return str[a]==str[b]&&str[a+1]==str[b+1]&&str[a+2]==str[b+2];
	}
	bool cmp3(int *str, int *nstr, int a, int b){
		if(str[a]!=str[b])return str[a]<str[b];
		if(str[a+1]!=str[b+1])return str[a+1]<str[b+1];
		return nstr[a+b%3]<nstr[b+b%3];
	}
	void radixsort(int *str, int *sa, int *res, int n, int m){
		int i;
		REP(i,m)id[i]=0;
		REP(i,n)++id[str[sa[i]]];
		REP(i,m)id[i+1]+=id[i];
		DOWN(i,n-1,0)res[--id[str[sa[i]]]]=sa[i];
	}
	void dc3(int *str, int *sa, int n, int m){
		#define F(x) ((x)/3+((x)%3==1?0:one))
		#define G(x) ((x)
		int *nstr=str+n, *nsa=sa+n, *tmpa=rank, *tmpb=height;
		int i,j,k,len=0,num=0,zero=0,one=(n+1)/3;
		REP(i,n)if(i%3)tmpa[len++]=i;
		str[n]=str[n+1]=0;
		radixsort(str+2, tmpa, tmpb, len, m);
		radixsort(str+1, tmpb, tmpa, len, m);
		radixsort(str+0, tmpa, tmpb, len, m);
		nstr[F(tmpb[0])]=num++;
		UPTO(i,1,len-1)
			nstr[F(tmpb[i])]=equal(str,tmpb[i-1],tmpb[i])?num-1:num++;
		if(num<len)dc3(nstr,nsa,len,num);
		else REP(i,len)nsa[nstr[i]]=i;
		if(n%3==1)tmpa[zero++]=n-1;
		REP(i,len)if(nsa[i]<one)tmpa[zero++]=nsa[i]*3;
		radixsort(str, tmpa, tmpb, zero, m);
		REP(i,len)tmpa[nsa[i]=G(nsa[i])]=i;
		i=j=0;
		REP(k,n)
		if(j>=len||(i<zero&&cmp3(str,tmpa,tmpb[i],nsa[j])))sa[k]=tmpb[i++];
		else sa[k]=nsa[j++];
	}
	void initSA(T *s, int n,int m){
		int i,j,k=0;
		str[len=n]=0;//末尾增加一个0,这样就省去一些特殊情况的讨论,也就是最后一个mod 3刚好等于0
		REP(i,n)str[i]=s[i];
		dc3(str,sa,n+1,m);      //可以切换成dc3
		REP(i,n)sa[i]=sa[i+1];//第0小的默认为最后一个字符0,所以答案向前移动一位,da算法不用
        //da(str,sa,n,m);
		REP(i,n)rank[sa[i]]=i;
		REP(i,n)//计算height数组
		{
			if(k)--k;
			if(rank[i])for(j=sa[rank[i]-1];str[i+k]==str[j+k];++k);
			else k=0;
			height[rank[i]]=k;
		}
	}
	void initRMQ(){
		int i,j;
		int m=(int)(log(len*1.0)/log(2.0));
		REP(i,len)best[i][0]=height[i];
		for(j=1;j<=m;++j)
			for(i=0;i+(1<<j)-1<len;++i)
				best[i][j]=min(best[i][j-1],best[i+(1<<(j-1))][j-1]);
	}
	int RMQ(int l, int r){
		int k=int(log(r-l+1.0)/log(2.0));
		return min(best[l][k],best[r-(1<<k)+1][k]);
	}
	int LCPSA(int a, int b){//查询区间RMQ(i,j)
		a=rank[a],b=rank[b];
		if(a>b)swap(a,b);
		return RMQ(a+1,b);
	}
};
suffixarray<char,maxn> msa;
map<int ,int > mymap;   //计算m,m表示不同字符个数,如果是字母直接用256
char s[maxn];
int main(){
    while(scanf("%s",s)){
        if(s[0]=='.')break;
        int len= strlen(s);
        msa.initSA(s,len,256);
         int flag = 0;
        for (int i = 1; i <= len; i++) {
            if (len % i == 0 && msa.rank[0] == msa.rank[i] + 1 && msa.height[msa.rank[0]] == len - i) {
                printf("%d\n", len / i);
                flag = 1;
                break;
            }
        }
        if (!flag)printf("1\n");
    }

	return 0;
}

你可能感兴趣的:(字符串模版,后缀数组)