标签:rank targe 后缀数组 vector algorithm space %s define printf
#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <vector> #include <map> #include <cmath> #include <stack> #define F(x) ((x)/3+((x)%3==1?0:tb)) #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) using namespace std; const int MAXN = 1000105; const int inf = 1000000007; int wa[MAXN*3],wb[MAXN*3],wv[MAXN*3],wss[MAXN*3]; int sa[MAXN*3],Rank[MAXN*3],height[MAXN*3],r[MAXN*3]; char s[MAXN*3]; int query[MAXN*3]; int c0(int *r,int a,int b){ return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2]; } int c12(int k,int *r,int a,int b){ if(k==2){ return r[a]<r[b]||(r[a]==r[b]&&c12(1,r,a+1,b+1)); } else{ return r[a]<r[b]||(r[a]==r[b]&&wv[a+1]<wv[b+1]); } } void sort(int *r,int *a,int *b,int n,int m){ int i; for(i=0;i<n;i++){ wv[i]=r[a[i]]; } for(i=0;i<m;i++){ wss[i]=0; } for(i=0;i<n;i++){ wss[wv[i]]++; } for(i=1;i<m;i++){ wss[i]+=wss[i-1]; } for(i=n-1;i>=0;i--){ b[--wss[wv[i]]]=a[i]; } } void dc3(int *r,int *sa,int n,int m){ int i,j,*rn=r+n; int *san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p; r[n]=r[n+1]=0; for(i=0;i<n;i++){ if(i%3!=0){ wa[tbc++]=i; } } sort(r+2,wa,wb,tbc,m); sort(r+1,wb,wa,tbc,m); sort(r,wa,wb,tbc,m); for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++){ rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++; } if(p<tbc){ dc3(rn,san,tbc,p); } else{ for(int i=0;i<tbc;i++){ san[rn[i]]=i; } } for(i=0;i<tbc;i++){ if(san[i]<tb){ wb[ta++]=san[i]*3; } } if(n%3==1){ wb[ta++]=n-1; } sort(r,wb,wa,ta,m); for(i=0;i<tbc;i++){ wv[wb[i]=G(san[i])]=i; } for(i=0,j=0,p=0;i<ta&&j<tbc;p++){ sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++]; } for(;i<ta;p++){ sa[p]=wa[i++]; } for(;j<tbc;p++){ sa[p]=wb[j++]; } } void getheight(int *r,int *sa,int n) { int i,j,k=0; for(i=0; i<n; i++) r[i]=s[i]; r[i]=0; dc3(r,sa,n+1,128); 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++); } int main() { while(scanf("%s",s)) { if(!strcmp(s,".")){ break; } int n=strlen(s); getheight(r,sa,n); query[Rank[0]]=inf; int i; for(i=Rank[0]-1; i>=1; i--) query[i]=min(height[i+1],query[i+1]); for(i=Rank[0]+1; i<=n; i++) query[i]=min(height[i],query[i-1]); for(i=1; i<=n; i++) if(n%i==0&&query[Rank[i]]==n-i) break; printf("%d\n",n/i); } return 0; }
标签:rank targe 后缀数组 vector algorithm space %s define printf
原文地址:https://www.cnblogs.com/Accpted/p/11357105.html