zoukankan      html  css  js  c++  java
  • 后缀数组板子

     1 #include<cstdio>
     2 #include<algorithm>
     3 #define LL long long
     4 #define fi first
     5 #define se second
     6 #define mk make_pair
     7 #define pii pair<int,int>
     8 
     9 using namespace std;
    10 
    11 const int N=2e5+7;
    12 const int M=1e4+7;
    13 const int inf=0x3f3f3f3f;
    14 const LL INF=0x3f3f3f3f3f3f3f3f;
    15 const int mod=1e9 + 7;
    16 
    17 char s[N];
    18 int sa[N], t[N], t2[N], c[N], rk[N], height[N], n;
    19 
    20 void buildSa(int n, int m) {
    21     int i, j = 0, k = 0, *x = t, *y = t2;
    22     for(i = 0; i < m; i++) c[i] = 0;
    23     for(i = 0; i < n; i++) c[x[i] = s[i]]++;
    24     for(i = 1; i < m; i++) c[i] += c[i - 1];
    25     for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
    26     for(int k = 1; k <= n; k <<= 1) {
    27         int p = 0;
    28         for(i = n - k; i < n; i++) y[p++] = i;
    29         for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
    30         for(i = 0; i < m; i++) c[i] = 0;
    31         for(i = 0; i < n; i++) c[x[y[i]]]++;
    32         for(i = 1; i < m; i++) c[i] += c[i - 1];
    33         for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
    34         swap(x, y);
    35         p = 1; x[sa[0]] = 0;
    36         for(int i = 1; i < n; i++) {
    37             if(y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k])
    38                 x[sa[i]] = p - 1;
    39             else x[sa[i]] = p++;
    40         }
    41         if(p >= n) break;
    42         m = p;
    43      }
    44 
    45      for(i = 1; i < n; i++) rk[sa[i]] = i;
    46      for(i = 0; i < n - 1; i++) {
    47         if(k) k--;
    48         j = sa[rk[i] - 1];
    49         while(s[i + k] == s[j + k]) k++;
    50         height[rk[i]] = k;
    51      }
    52 }
    53 int main() {
    54     scanf("%s", s);
    55     n = strlen(s);
    56     buildSa(n + 1, 180);
    57     return 0;
    58 }
  • 相关阅读:
    Magento:Paypal付款不成功返回后不要清空购物车产品的解决方案
    magento设置订单状态
    linux下查看所有用户以及用户组
    网站无法访问的原因
    magento 多域名多店铺
    linode空间lamp环境的搭建
    hp p410固件更新
    tracert
    镜像架设
    nohup
  • 原文地址:https://www.cnblogs.com/CJLHY/p/8980467.html
Copyright © 2011-2022 走看看