poj 1743 -- Musical Theme (后缀数组模板题)

就当存个模板

# include <set>
# include <map>
# include <list>
# include <queue>
# include <stack>
# include <cmath>
# include <string>
# include <cstdio>
# include <vector>
# include <cstdlib>
# include <cstring>
# include <iostream>
# include <algorithm>

using namespace std ;

# define PB             push_back
# define SIZE(x)        (int)x.size()
# define clr(x,y)       memset(x,y,sizeof(x))
# define MP(x,y)        make_pair(x,y)
# define read(n)        scanf ( "%d" , & (n) )
# define reads(n)       scanf ( "%s" , n ) ;
# define ALL(t)         (t).begin(),(t).end()
# define FOR(i,n,m)     for ( int i = n ; i <= m ; i ++ )
# define code(x)        (x-'a')
# define IT             iterator

typedef long long               ll;
typedef unsigned int            uint;
typedef unsigned long long      ull;
typedef vector<int>             vint;
typedef vector<string>          vstring;

const ll mod = 1e9+7 ;
const ll LINF = 1e18 ;
const int INF = 1e9 ;
const double EPS = 1e-8;

const int maxn = 200005;
/*
字符要先转化为正整数
待排序的字符串放在r 数组中,从r[0]到r[n-1],长度为n,且最大值小于m。
所有的r[i]都大于0,r[n]无意义算法中置0
函数结束后,结果放在sa 数组中(名次从1..n),从sa[1]到sa[n]。s[0]无意义
*/
int sa[maxn],r[maxn];
int cmp(int *r,int a,int b,int l)//
{
        return r[a]==r[b]&&r[a+l]==r[b+l];
}
int wa[maxn],wb[maxn],wv[maxn],ws1[maxn];
void makesa (int n,int m)//
{
        r[n++]=0;
        int i,j,p,*x=wa,*y=wb,*t;
        for(i=0; i<m; i++)ws1[i]=0;
        for(i=0; i<n; i++)ws1[x[i]=r[i]]++;
        for(i=1; i<m; i++)ws1[i]+=ws1[i-1];
        for(i=n-1; i>=0; i--)sa[--ws1[x[i]]]=i;
        for(j=1,p=1; p<n; j*=2,m=p) {
                for(p=0,i=n-j; i<n; i++) y[p++]=i;
                for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
                for(i=0; i<n; i++) wv[i]=x[y[i]];
                for(i=0; i<m; i++) ws1[i]=0;
                for(i=0; i<n; i++) ws1[wv[i]]++;
                for(i=1; i<m; i++) ws1[i]+=ws1[i-1];
                for(i=n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i];
                for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
                        x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
}
/*
height[2..n]:height[i]保存的是lcp(sa[i],sa[i-1])
rank[0..n-1]:rank[i]保存的是原串中suffix[i]的名次
*/
int rank[maxn],height[maxn];
void calheight(int n)
{
        int i,j,k=0;
        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 lcp(int i,int j)//原串中suffix[i]与suffix[j]的lcp
{
        //rmq解决
        //rank[i]<rank[j]
        //return min(height[ rank[i]+1 ]......height[ rank[j] ])
        return 0;
}

char s[maxn] ;
int n;

bool check (int len) {
        int minn = INF, maxx = 0;
        FOR (i,2,n) {
                if (height[i] < len) {
                        minn = INF, maxx = 0;
                } else {
                        minn = min (minn,min (sa[i],sa[i-1]));
                        maxx = max (maxx,max (sa[i],sa[i-1]));
                        if (maxx - minn >= len) return true;
                }
        }
        return false;
}

int main(){
        while (cin >> n,n) {
                FOR (i,0,n-1) read (r[i]);
                for (int i = n - 1 ; i ; i --) {
                        r[i] = r[i] - r[i - 1] + 90;
                }
                makesa (n,200);
                calheight (n);
                int low = 0 , high = n ;
                while (low <= high) {
                        int mid = (low + high) / 2;
                        if (check (mid))        low = mid + 1;
                        else    high = mid - 1;
                }
                if (low < 5)    cout << 0 << endl;
                else    cout << low << endl;
        }
}


你可能感兴趣的:(poj 1743 -- Musical Theme (后缀数组模板题))