【接尾辞配列と統計】


接尾辞配列は従来強力であり,その統計機能は一方である.
次は2つのテーマを例に、強力な接尾辞配列を使用して統計する方法を説明します.
【例1】POJ 3415http://poj.org/problem?id=3415
この問題は2つの列の中の長さを求めます>=Kのサブ列の個数(繰り返し可能)
方法:まず最も正しい方法は接尾辞の配列で2つの列をリンクして、中間に現れたことがない文字をプラスして、新しい列に対してheighの配列を求めて、私達は先にAに出会ったことに対して前とBのlcpに対して加算することができて、同様にBに属することに出会っても前とAのlcpに対して加算して、今問題はどのように加算して、O(n^2)アルゴリズムは明らかに実行できませんか?「単調スタック」と呼ばれるものがあり、その要素はその名の通り単調である.単調スタックの最も一般的な用途はサブマトリクスの最大面積を求めることであり、現在も同様である.任意の2つの接尾辞のlcpは区間の最小値であるからである.より具体的にはこのblogを参照することができる.http://www.cnblogs.com/Booble/archive/2010/12/14/1906147.htmlちなみに、現在のlcpの最小値hの区間数を1つの配列で保存することができ、スタック操作と同時に更新することができます.
#define maxn 200100
int wa[maxn],wb[maxn],wv[maxn],wss[maxn];
int r[maxn],sa[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b] && r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m){
     int i,j,p,*x=wa,*y=wb,*t;
     for(i=0;i=0;i--) sa[--wss[x[i]]]=i;
     for(j=1,p=1;p=j) y[p++]=sa[i]-j;
         for(i=0;i=0;i--) sa[--wss[wv[i]]]=y[i];
         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i=k) ? height[i]-k+1 : 0;
        }
        LL ans = 0;
        st[0] = -1,fa[len+1] = 0;
        for(j=0;j<=1;j++){
            LL sum = 0;
            for(int top = 0,i=2;i<=len;i++){
                if(fb[i]!=j)ans+=sum;
                st[++top] = fa[i+1];
                fc[top] = (fb[i]==j);
                sum += (LL)st[top]*(LL)fc[top];
                while(st[top-1]>=st[top]){
                    sum -= (LL)(st[top-1]-st[top])*(LL)fc[top-1];
                    st[top-1] = st[top];
                    fc[top-1] += fc[top];//    
                    top--;
                }
            }
        }
        printf("%I64d
",ans); } return 0; }

【例2】
E. Prefix Sum
Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 88   Accepted Submission(s) : 14
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
A string v is a suffix string of a string w if string v can read from a position of string w and to the end of w.
For example, string bc is a suffix string of abc. but ab is not.
A string v is a prefix string of a string w if string v can read from the beginning of string w.
For example, string ab is prefix string of string abc, but bc and abcd are not.
For 2 strings s1 and s2, if there is a string s3 is both the prefix of s1 and s2, we call s3 is a common prefix of s1 and s2.
The longest common prefix of 2 strings is the longest common prefix string of all the common prefix strings among these 2 strings.
Your task is:
Give you the string, count the sum of the length of each of the longest common prefix string of each 2 suffix of the string.
Input
There are multi strings. One string per line. Each string is no longer than 10^5. The strings only contain A-Z and a-z.
Output
For each string, output the sum.
Sample Input
ABC
ABABA
AABB

Sample Output
0
7
2

Source
SCAUCPC 2012
これは華農校試合の「難題」であり、実は難しくなく、接尾辞配列で完全にできる.
題意は簡単です:1つの列を与えて、2つの接尾辞の最も長い接頭辞の長さを計算して、そして和を求めます. 
方法:接尾辞配列+単調スタック最適化
#define maxn 100100
int wa[maxn],wb[maxn],wv[maxn],wss[maxn];
int r[maxn],sa[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b] && r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m){
     int i,j,p,*x=wa,*y=wb,*t;
     for(i=0;i=0;i--) sa[--wss[x[i]]]=i;
     for(j=1,p=1;p=j) y[p++]=sa[i]-j;
         for(i=0;i=0;i--) sa[--wss[wv[i]]]=y[i];
         for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i=st[top]){
                st[top-1] = st[top];
                c[top-1] += c[top];
                top--;
            }
            ans += c[top]*st[top]+dp[top-1];
            dp[top] = dp[top-1]+c[top]*st[top];
        }
        printf("%I64d
",ans); } return 0; }