zoukankan      html  css  js  c++  java
  • [SPOJ 687]Repeats

    Description

    题库链接

    给出一个长度为 (n) 的字符串,求重复次数最多的连续重复子串。

    (1leq nleq 50000)

    Solution

    Code

    #include <bits/stdc++.h>
    #define log2 LOG
    using namespace std;
    const int N = 100000+5, inf = ~0u>>1;
    
    char ch[N];
    int n, m, x[N<<1], y[N<<1], c[N], sa[N], rk[N], height[N], Case;
    int log2[N], bin[30], f[30][N], ans, t;
    
    void get() {
        for (int i = 1; i <= m; i++) c[i] = 0;
        for (int i = 1; i <= n; i++) c[x[i] = ch[i]]++;
        for (int i = 2; i <= m; i++) c[i] += c[i-1];
        for (int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
        for (int k = 1; k <= n; k <<= 1) {
            int num = 0;
            for (int i = n-k+1; i <= n; i++) y[++num] = i;
            for (int i = 1; i <= n; i++) if (sa[i] > k) y[++num] = sa[i]-k;
            for (int i = 1; i <= m; i++) c[i] = 0;
            for (int i = 1; i <= n; i++) c[x[i]]++;
            for (int i = 2; i <= m; i++) c[i] += c[i-1];
            for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
            swap(x, y); x[sa[1]] = num = 1;
            for (int i = 2; i <= n; i++)
                x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]) ? num : ++num;
            if ((m = num) == n) break;
        }
        for (int i = 1; i <= n; i++) rk[sa[i]] = i;
        for (int i = 1, k = 0; i <= n; i++) {
            if (rk[i] == 1) continue;
            if (k) --k; int j = sa[rk[i]-1];
            while (j+k <= n && i+k <= n && ch[i+k] == ch[j+k]) ++k;
            height[rk[i]] = k;
        }
    }
    void rmq() {
        int t = log2[n];
        for (int i = 1; i <= n; i++) f[0][i] = height[i];
        for (int i = 1; i <= t; i++)
            for (int j = 1; j+bin[i]-1 <= n; j++)
                f[i][j] = min(f[i-1][j], f[i-1][j+bin[i-1]]);
    }
    int query(int a, int b) {
        a = rk[a], b = rk[b];
        if (a > b) swap(a, b); ++a;
        int t = log2[b-a+1];
        return min(f[t][a], f[t][b-bin[t]+1]);
    }
    void work() {
        bin[0] = 1; log2[0] = -1;
        for (int i = 1; i <= 25; i++) bin[i] = (bin[i-1]<<1);
        for (int i = 1; i < N; i++) log2[i] = log2[i>>1]+1;
        scanf("%d", &t);
        while (t--) {
            scanf("%d", &n); getchar(); m = 255; ans = 0;
            for (int i = 1; i <= n; i++) scanf("%c", &ch[i]), getchar();
            get(); rmq();
            for (int l = 1; l <= n; l++)
                for (int i = 1; i+l <= n; i += l) {
                    int k = query(i, i+l), t = l-k%l, p = i-t, m = k/l+1;
                    if (p > 0 && query(p+l, p) >= l-k%l) ++m;
                    if (m > ans) ans = m;
                }
            printf("%d
    ", ans);
        } 
    }
    int main() {work(); return 0; }
  • 相关阅读:
    git pull fatal: refusing to merge unrelated histories
    Git报错:Your branch is ahead of 'origin/master' by 1 commit
    java读取大文件内容到Elasticsearch分析(手把手教你java处理超大csv文件)
    Java try catch语句块中try()的括号中代码作用
    Redis这15个“雷坑”,别问我咋知道的……
    MySql死锁
    MQ限流应用
    什么是 JWT -- JSON WEB TOKEN
    mybatis-plus查询指定字段
    数据库炸了----我就重启了一下啊(Communications link failure)
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/9274228.html
Copyright © 2011-2022 走看看