• LA 6856 Circle of digits 解题报告


    题目链接

      先用后缀数组给串排好序。dc3 O(n)

          二分答案+贪心check

      答案的长度len=(n+k-1)/k

          如果起点为i长为len串大于当前枚举的答案,i的长度取len-1

      从起点判断k个串的长度是否大于等于n

          check的时候最多枚举len个起点,每个位置需要枚举n/len个串,时间复杂度O(n),总的时间复杂度O(nlogn+n)

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <fstream>
    using namespace std;
    #define ll long long
    #define MAXN 1000009
    int sa[MAXN], pos[MAXN], Rank[MAXN], r[MAXN];
    int d, n, k;
    string s;
    #define F(x) ((x)/3+((x)%3==1?0:tb))
    #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
    int wa[MAXN], wb[MAXN], wv[MAXN], Ws[MAXN];
    inline int c0 (int *r, int a, int b) {
        return r[a] == r[b] && r[a + 1] == r[b + 1] && r[a + 2] == r[b + 2];
    }
    int c12 (int k, int *r, int a, int b) {
        if (k == 2) return r[a] < r[b] || r[a] == r[b] && c12 (1, r, a + 1, b + 1);
        else return r[a] < r[b] || r[a] == r[b] && wv[a + 1] < wv[b + 1];
    }
    inline void sort (int *r, int *a, int *b, int n, int m)
    {
        int i;
        for (i = 0; i < n; i++) wv[i] = r[a[i]];
        for (i = 0; i < m; i++) Ws[i] = 0;
        for (i = 0; i < n; i++) Ws[wv[i]]++;
        for (i = 1; i < m; i++) Ws[i] += Ws[i - 1];
        for (i = n - 1; i >= 0; i--) b[--Ws[wv[i]]] = a[i];
        return;
    }
    inline void dc3 (int *r, int *sa, int n, int m)
    {
        int i, j, *rn = r + n, *san = sa + n, ta = 0, tb = (n + 1) / 3, tbc = 0, p;
        r[n] = r[n + 1] = 0;
        for (i = 0; i < n; i++) if (i % 3 != 0) wa[tbc++] = i;
        sort (r + 2, wa, wb, tbc, m);
        sort (r + 1, wb, wa, tbc, m);
        sort (r, wa, wb, tbc, m);
        for (p = 1, rn[F (wb[0])] = 0, i = 1; i < tbc; i++)
            rn[F (wb[i])] = c0 (r, wb[i - 1], wb[i]) ? p - 1 : p++;
        if (p < tbc) dc3 (rn, san, tbc, p);
        else for (i = 0; i < tbc; i++) san[rn[i]] = i;
        for (i = 0; i < tbc; i++) if (san[i] < tb) wb[ta++] = san[i] * 3;
        if (n % 3 == 1) wb[ta++] = n - 1;
        sort (r, wb, wa, ta, m);
        for (i = 0; i < tbc; i++) wv[wb[i] = G (san[i])] = i;
        for (i = 0, j = 0, p = 0; i < ta && j < tbc; p++)
            sa[p] = c12 (wb[j] % 3, r, wa[i], wb[j]) ? wa[i++] : wb[j++];
        for (; i < ta; p++) sa[p] = wa[i++];
        for (; j < tbc; p++) sa[p] = wb[j++];
        return;
    }
    inline bool check (int x) {
        for (int i = 0; i < d; i++) {
            int j = i, t = 0;
            while (t < k) {
                if (Rank[j % n] <= x) j += d;
                else
                    j += d - 1;
                t++;
            }
            if (j - i >= n) return 1;
        }
        return 0;
    }
    int main() {
        while (scanf ("%d %d", &n, &k) == 2) {
            cin >> s; s += s;
            d = (n + k - 1) / k;
            for (int i = 0; i < (n << 1); i++) r[i] = s[i] - '0';
            r[n << 1] = 0;
            dc3 (r, sa, s.size() + 1, 10);
            for (int i = 1, p = 0; i <= (n << 1); i++)
                Rank[sa[i]] = ++p;
            for (int i = 1, p = 0; i <= (n << 1); i++)
                if (sa[i] < n ) pos[++p] = sa[i];
            int l = 1, r = n, last = -1;
            while (l <= r) {
                int mid = (l + r) >> 1;
                if (check (Rank[pos[mid]]) ) last = pos[mid], r = mid - 1;
                else
                    l = mid + 1;
            }
            for (int i = last; i < last + d; i++)
                putchar (s[i]);
            putchar (10);
        }
    }
    View Code
  • 相关阅读:
    如何做实时监控?—— 参考 Spring Boot 实现
    如何做实时监控?—— 参考 Spring Boot 实现
    spring boot application properties配置详解
    Jrebel 6.2.1破解
    智能社-JS -wiki
    hibernate.properties
    Tomcat 的 socket bind failed的解决方法
    js 排序 SORT 各种方法
    java EE 如何使用Eclipse启动一个项目
    2016-06-06 数组的几个重要方法
  • 原文地址:https://www.cnblogs.com/keam37/p/4293616.html
Copyright © 2020-2023  润新知