标签:cal std str math space lib ring == ++
https://blog.csdn.net/qq_35640373/article/details/70168683
1 //hdu 3518
2 #include 3 #include string.h>
4 #include 5 #include 6 #define LL long long
7 using namespace std;
8
9 #define maxn 1020
10 int max(int x,int y){ return x>y?x:y;}
11 int min(int x,int y){ return xx:y;}
12 int wa[maxn],wb[maxn],wv[maxn],WS[maxn];
13
14 int cmp(int *r,int a,int b,int l){
15 return r[a]==r[b]&&r[a+l]==r[b+l];
16 }
17
18 void da(int *r,int *sa,int n,int m){
19 int i,j,p,*x=wa,*y=wb,*t;
20 for(i=0;i0;
21 for(i=0;i;
22 for(i=1;i1];
23 for(i=n-1;i>=0;i--) sa[--WS[x[i]]]=i;
24 for(j=1,p=1;p2,m=p){
25 for(p=0,i=n-j;ii;
26 for(i=0;iif(sa[i]>=j) y[p++]=sa[i]-j;
27 for(i=0;ix[y[i]];
28 for(i=0;i0;
29 for(i=0;i;
30 for(i=1;i1];
31 for(i=n-1;i>=0;i--) sa[--WS[wv[i]]]=y[i];
32 for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i)
33 x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
34 }
35 return;
36 }
37
38 int Rank[maxn],height[maxn];
39
40 void calheight(int *r,int *sa,int n){
41 int i,j,k=0;
42 for(i=1;ii;
43 for(i=0;ik)
44 for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++);
45 return;
46 }
47
48 int r[maxn],sa[maxn];
49 char s[maxn];
50
51 int ok(int k,int n){
52 int i,j,ret=0;
53 int maxx,minx;
54 maxx=-1;
55 minx=9999999;
56 for(i=1;i){
57 if(height[i]>=k)//分组满足大于k的
58 {
59 maxx=max(maxx,sa[i]);
60 minx=min(minx,sa[i]);
61 }
62 else
63 {
64 if(maxx-minx>=k)//之前满足条件就要+1;
65 ret++;
66 maxx=sa[i];
67 minx=sa[i];
68 }
69 }
70 if(maxx-minx>=k)
71 ret++;
72 return ret;
73 }
74
75 int main(){
76 int i,j;
77 while(cin>>s){
78 if(s[0]==‘#‘) break;
79 int len=strlen(s);
80 for(i=0;i)
81 r[i]=s[i];
82 r[len]=0;
83 da(r,sa,len+1,123);
84 calheight(r,sa,len);
85 LL ans=0;
86 for(i=1;i){
87 ans+=ok(i,len);
88 }
89 coutendl;
90 }
91 return 0;
92 }
后缀数组
标签:cal std str math space lib ring == ++
原文地址:https://www.cnblogs.com/jaydenouyang/p/9562917.html