51NOD 1089 后缀数组 或 manacher

简略题意:求解最长回文子串。

经典问题,为了练习后缀数组再写一遍吧。
将原串和原串的反转串用没用过的字符拼接起来,发现原问题等价于求最大height[i], 且sa[i-1]和sa[i]分别在原串和反转串中。
复杂度O(nlogn)
嘿嘿嘿,刚从uoj搞了个sais的板子
复杂度O(n)。

#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int N = 300000;

int n;

namespace SA {
    int sa[N], rank[N], height[N], s[N<<1], t[N<<1], p[N], cnt[N], cur[N];
    int MIN[N][30];
    #define pushS(x) sa[cur[s[x]]--] = x
    #define pushL(x) sa[cur[s[x]]++] = x
    #define inducedSort(v) fill_n(sa, n, -1); fill_n(cnt, m, 0);                  \
        for (int i = 0; i < n; i++) cnt[s[i]]++;                                  \
        for (int i = 1; i < m; i++) cnt[i] += cnt[i-1];                           \
        for (int i = 0; i < m; i++) cur[i] = cnt[i]-1;                            \
        for (int i = n1-1; ~i; i--) pushS(v[i]);                                  \
        for (int i = 1; i < m; i++) cur[i] = cnt[i-1];                            \
        for (int i = 0; i < n; i++) if (sa[i] > 0 &&  t[sa[i]-1]) pushL(sa[i]-1); \
        for (int i = 0; i < m; i++) cur[i] = cnt[i]-1;                            \
        for (int i = n-1;  ~i; i--) if (sa[i] > 0 && !t[sa[i]-1]) pushS(sa[i]-1)
    void sais(int n, int m, int *s, int *t, int *p) {
        int n1 = t[n-1] = 0, ch = rank[0] = -1, *s1 = s+n;
        for (int i = n-2; ~i; i--) t[i] = s[i] == s[i+1] ? t[i+1] : s[i] > s[i+1];
        for (int i = 1; i < n; i++) rank[i] = t[i-1] && !t[i] ? (p[n1] = i, n1++) : -1;
        inducedSort(p);
        for (int i = 0, x, y; i < n; i++) if (~(x = rank[sa[i]])) {
            if (ch < 1 || p[x+1] - p[x] != p[y+1] - p[y]) ch++;
            else for (int j = p[x], k = p[y]; j <= p[x+1]; j++, k++)
                if ((s[j]<<1|t[j]) != (s[k]<<1|t[k])) {ch++; break;}
            s1[y = x] = ch;
        }
        if (ch+1 < n1) sais(n1, ch+1, s1, t+n, p+n1);
        else for (int i = 0; i < n1; i++) sa[s1[i]] = i;
        for (int i = 0; i < n1; i++) s1[i] = p[sa[i]];
        inducedSort(s1);
    }
    template<typename T>
    int mapCharToInt(int n, const T *str) {
        int m = *max_element(str, str+n);
        fill_n(rank, m+1, 0);
        for (int i = 0; i < n; i++) rank[str[i]] = 1;
        for (int i = 0; i < m; i++) rank[i+1] += rank[i];
        for (int i = 0; i < n; i++) s[i] = rank[str[i]] - 1;
        return rank[m];
    }
    template<typename T>
    void suffixArray(int n, const T *str) {
        int m = mapCharToInt(++n, str);
        sais(n, m, s, t, p);
        for (int i = 0; i < n; i++) rank[sa[i]] = i;
        for (int i = 0, h = height[0] = 0; i < n-1; i++) {
            int j = sa[rank[i]-1];
            while (i+h < n && j+h < n && s[i+h] == s[j+h]) h++;
            if (height[rank[i]] = h) h--;
        }
    }
    void RMQ_init(){
        for(int i=0; i<n; i++) MIN[i][0] = height[i+1];
        for(int j=1; (1<<j)<=n; j++){
            for(int i=0; i+(1<<j)<=n; i++){
                MIN[i][j] = min(MIN[i][j-1], MIN[i+(1<<(j-1))][j-1]);
            }
        }
    }
    int RMQ(int L, int R){
        int k = 0;
        while((1<<(k+1)) <= R-L+1) k++;
        return min(MIN[L][k], MIN[R-(1<<k)+1][k]);
    }
    int LCP(int i, int j){
         if(rank[i] > rank[j]) swap(i, j);
        return RMQ(rank[i], rank[j]-1);
    }
    void init(char *str, int _n){
        n = _n;
        suffixArray(n, str);
        RMQ_init();
    }
    void solve(int len) {
        int ans = 0;
        for(int i = 2; i <= n; i++) {
            int u = sa[i-1], v = sa[i];
            if(u > v) swap(u, v);
            if(u <= len && v > len)
                ans = max(ans, height[i]);
        }
        printf("%d\n", ans);
    }
};

int t;
char str[220000], str2[110000];

int main() {
    scanf("%s", str);
    strcpy(str2, str);
    int len1 = strlen(str);
    int len2 = len1;
    reverse(str2, str2+len2);
    str[len1] = '@';
    str[len1+1] = '\0';
    len1++;
    strcat(str, str2);
    SA::init(str, len1+len2);
    SA::solve(len1-2);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值