Suffix array:

1, the difference:
the card in two places.
1 is equal to the count value may be more or omission. To Shoumo it. Not simply equal to the cover, written in a top. x == y, idx <idy, L , and different id. y of L should be recorded as idx + 1,
otherwise it will be multi-operator, repeat. So monotonous stack is <=.
2 is to calculate the L, R's. (i-id [top]) before writing the i-id + 1.
Properties: Rank is i, the suffix j (i <j) of the lcp is min {height [k] | i + 1 ≤ k ≤ j}
sigma (leni, j) = ( n-1) * (n + 1) * n / 2. n-1 is calculations are each n-1 times. Followed by a prefix and.
Translation: seeking lcp. Seeking heigh [i] becomes the minimum interval.
    Monotonous stack maintenance. Small difficulty is equal to the value of Shoumo it.

#include <cstdio> 
#include <CString> 
#include <algorithm>
 #define F. (I, A, B) for (int I = Register A; I <= B; I ++)
 #define forb (I, A, B) for (int I = Register A; I> = B; - I)
 #define IL inline
 #define LL Long Long
 #define RG Register
 #define PF (A) the printf ( "% D", A)
 #define the PF (A) printf ( "% LLD", A)
 #define PHN the puts ( "")
 a using  namespace std;
 int the Read ();
 / * 
card in two places. 
1 is equal to the count value may be more or omission. To Shoumo it. Not simply equal to the cover, written in a top. x == y, idx <idy, L , and different id. The y L +. 1 should be referred to as IDX,  
otherwise it will be multi-operator, repeat. So monotonous stack is <=.
2 is to calculate the L, R's. (i-id [top]) is written before the i-id + 1.
Properties: Rank is i, the suffix j (i <j) of the lcp is min {height [K] | I +. 1 ≤ K ≤ J} 
Sigma (Leni, J) = (n--. 1) * (n-+. 1) * n / 2. n-1 is calculations are each n-1 times. Followed by a prefix and. 
Translation: seeking lcp. Seeking heigh [i] becomes the minimum interval. 
    Monotonous stack maintenance. Small difficulty is equal to the value of Shoumo it. 
* / 
#Define NX 500 010
 int n-, m;
 char STR [NX];
 int A [NX], SA [NX], height [NX], RK [NX], Tax [NX], TP [NX];
 void Rsort () { 
    F. (I, 0 , m) Tax [I] = 0 ; 
    F. (I, . 1 , n-) ++ Tax [RK [TP [I]]]; 
    F. (I, . 1 , m) Tax [I ] + = Tax [I- . 1 ]; 
    forb (I, n-, . 1 ) SA [Tax [RK [TP [I]]] -] = TP [I]; 
} 
int cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w];}
void Suffix(){
    F(i,1,n)rk[i]=a[i],tp[i]=i;
    m=127;Rsort();
    for(int w=1,p=1,i;p<n;w<<=1,m=p){
        for(p=0,i=n-w+1;i<=n;++i)tp[++p]=i;
        F(i,1,n)if(sa[i]>w)tp[++p]=sa[i]-w;
        Rsort();swap(rk,tp);rk[sa[1]]=p=1;
        F(i,2,n)rk[sa[i]]=cmp(tp,sa[i],sa[i-1],w)?p:++p;
    }
    int j,k=0;
    for(int i=1;i<=n;height[rk[i++]]=k)
        for(k=k?k-1:k,j=sa[rk[i]-1];a[i+k]==a[j+k];++k);
}
int sta[NX],id[NX],bg[NX],top;
int main(){
    scanf("%s",str+1);
    n=strlen(str+1);
    F(i,1,n)a[i]=str[i];
    Suffix();
    LL ans=1ll*(n-1)*(n+1)*n/2;
  //  F(i,1,n)pf(rk[i]);phn;
  //  F(i,1,n)pf(height[i]);phn;
    height[n+1]=-1;
  //  PF(ans);
    F(i,1,n+1){
        while(top&&sta[top]>height[i]){
            ans-=2ll*(id[top]-bg[top])*(i-id[top])*sta[top--];//PF(ans);
        }
        if(!top){
            sta[++top]=height[i];id[top]=i;bg[top]=0;
        }
        else{
        //    if(sta[top]!=height[i]){
               sta[++top]=height[i];id[top]=i;bg[top]=id[top-1];
        //    }
        }
    }
    printf("%lld",ans);
}
il int read(){
    rg int s=0,f=0;rg char ch;
    while(ch=getchar(),ch=='-'?f=1:0,ch<'0'||ch>'9');
    while(ch>='0'&&ch<='9'){
        s=s*10+(ch^48);ch=getchar();
    }
    return s;
}
/*
g++ 1.cpp -g
./a.out
eededeedeedeedde

*/
View Code

 

Guess you like

Origin www.cnblogs.com/seamtn/p/11297648.html