poj1743 后缀数组, poj挂了 存个代码
Posted notnight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1743 后缀数组, poj挂了 存个代码相关的知识,希望对你有一定的参考价值。
#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挂了 存个代码的主要内容,如果未能解决你的问题,请参考以下文章