码迷,mamicode.com
首页 > 编程语言 > 详细

poj1743 后缀数组, poj挂了 存个代码

时间:2018-07-20 21:42:48      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:poj   std   main   wap   col   builds   using   --   define   

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg

using namespace std;

const int N = 1e5 + 7;
const int M = 1e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;

int s[N], a[N], n;
int sa[N], t[N], t2[N], c[N], rk[N], height[N];

void buildSa(int *s, int n, int m) {
    int i, j = 0, k = 0, *x = t, *y = t2;
    for(i = 0; i < m; i++) c[i] = 0;
    for(i = 0; i < n; i++) c[x[i] = s[i]]++;
    for(i = 1; i < m; i++) c[i] += c[i - 1];
    for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
    for(int k = 1; k <= n; k <<= 1) {
        int p = 0;
        for(i = n - k; i < n; i++) y[p++] = i;
        for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
        for(i = 0; i < m; i++) c[i] = 0;
        for(i = 0; i < n; i++) c[x[y[i]]]++;
        for(i = 1; i < m; i++) c[i] += c[i - 1];
        for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for(int i = 1; i < n; i++) {
            if(y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k])
                x[sa[i]] = p - 1;
            else x[sa[i]] = p++;
        }
        if(p >= n) break;
        m = p;
     }

     for(i = 1; i < n; i++) rk[sa[i]] = i;
     for(i = 0; i < n - 1; i++) {
        if(k) k--;
        j = sa[rk[i] - 1];
        while(s[i + k] == s[j + k]) k++;
        height[rk[i]] = k;
     }
}

struct ST
{
    int dp[N][20], mm[N], ty;
    void build(int n,int b[],int _ty)
    {
        ty=_ty;
        for(int i=-(mm[0]=-1);i<N;i++)
            mm[i]=mm[i-1]+((i&(i-1))==0);
        for(int i=1;i<=n;i++)
            dp[i][0]=ty*b[i];
        for(int j=1;j<=mm[n];j++)
            for(int i=1;i+(1<<j)-1<=n;i++)
                dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
    }
    int query(int x,int y)
    {
        int k=mm[y-x+1];
        return ty*max(dp[x][k],dp[y-(1<<k)+1][k]);
    }
}rmqMx, rmqMn, rmq;

bool check(int len) {
    for(int i = 1, j = 1; i < n; i++) {
        j = max(i, j);
        while(j < n && rmq.query(i + 1, j + 1) >= len) j++;
        if(i != j) {
            int mx = rmqMx.query(i + 1, j);
            int mn = rmqMn.query(i + 1, j);

            if(mx > sa[i] + len) return true;
            if(sa[i] > mn + len) return true;
        }
    }
    return false;
}

int main() {
    while(scanf("%d", &n) != EOF && n) {

        for(int i = 0; i < n; i++) {
            scanf("%d", &a[i]);
        }

        if(n < 10) {
            puts("0");
        } else {

            for(int i = 0; i < n - 1; i++) {
                s[i] = a[i + 1] - a[i] + 100;
            }
            n--; s[n] = 0;

            buildSa(s, n + 1, 200);
            rmqMx.build(n, sa, 1);
            rmqMn.build(n, sa, -1);
            rmq.build(n, height, -1);

            int l = 4, r = n / 2, mid, ans = -1;

            while(l <= r) {
                mid = l + r >> 1;
                if(check(mid)) ans = mid, l = mid + 1;
                else r = mid - 1;
            }

            if(ans == -1) puts("0");
            else printf("%d\n", ans + 1);
        }
    }
    return 0;
}


/*
3 1000000000
100000 1000000000

507109376
*/

 

poj1743 后缀数组, poj挂了 存个代码

标签:poj   std   main   wap   col   builds   using   --   define   

原文地址:https://www.cnblogs.com/CJLHY/p/9343656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!