1. 程式人生 > >BZOJ4310: 跳蚤 【字尾陣列+二分】

BZOJ4310: 跳蚤 【字尾陣列+二分】

Description

很久很久以前,森林裡住著一群跳蚤。一天,跳蚤國王得到了一個神祕的字串,它想進行研究。首先,他會把串

分成不超過 k 個子串,然後對於每個子串 S,他會從S的所有子串中選擇字典序最大的那一個,並在選出來的k個子串中選擇字典序最大的那一個。他稱其為“魔力串”。現在他想找一個最優的分法讓“魔力串”字典序最小。

Input

第一行一個整數 k,K<=15

接下來一個長度不超過 10^5 的字串 S。

Output

輸出一行,表示字典序最小的“魔力串”。

Sample Input

2
ababa

Sample Output

ba
//解釋:
分成aba和ba兩個串,其中字典序最大的子串為ba


思路

首先我們要讓所有段的最大子串的最大串最小,然後就可以考慮用二分,因為有一大堆子串的操作

不難想到字尾陣列

然後就可以考慮怎麼check

我們從後向前貪心

每次因為只需要向前擴充套件一個位置,所以每次只用檢查一個子串是不是大於當前二分出的串

然後就可以很方便地做出來了


height處理的時候老是要寫錯

然後求lcp的時候注意把左區間的指標右移,並且要特判兩個串的起始位置相等的情況

然後是貪心的時候每一段最後一個字元一定要特判


#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> pi;
typedef long long ll;
const int N = 1e5 + 10;
const int LOG = 20;

struct Suffix_Array {
  int s[N], n, m;
  int c[N], x[N], y[N];
  int height[N], sa[N], rank[N];
  int st[N][LOG], Log[N];
  ll rank_pre[N]; 
  
  void init(int len, char *c) {
    n = len, m = 0;
    for (int i = 1; i <= len; i++) {
      s[i] = c[i];
      m = max(m, s[i]);
    }
  }
  
  void radix_sort() {
    for (int i = 1; i <= m; i++) c[i] = 0;
    for (int i = 1; i <= n; i++) c[x[y[i]]]++;
    for (int i = 1; i <= m; i++) c[i] += c[i - 1];
    for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
  }
  
  void buildsa() {
    for (int i = 1; i <= n; i++) x[i] = s[i], y[i] = i;
    radix_sort();
    int now;
    for (int k = 1; k <= n; k <<= 1) {
      now = 0;
      for (int i = n - k + 1; i <= n; i++) y[++now] = i;
      for (int i = 1; i <= n; i++) if (sa[i] > k) y[++now] = sa[i] - k;
      radix_sort();
      y[sa[1]] = now = 1;
      for (int i = 2; i <= n; i++) y[sa[i]] = (x[sa[i]] == x[sa[i - 1]] && x[sa[i] + k] == x[sa[i - 1] + k]) ? now : ++now;
      swap(x, y);
      if (now == n) break;
      m = now;
    }
  }
  
  void buildrank() {
    for (int i = 1; i <= n; i++) rank[sa[i]] = i;
  }
  
  void buildrank_pre() {
    for (int i = 1; i <= n; i++) rank_pre[i] = rank_pre[i - 1] + n - sa[i] + 1 - height[i];
  }

  void buildheight() {
    for (int i = 1; i <= n; i++) if (rank[i] != 1) {
      int k = max(height[rank[i - 1]] - 1, 0); // 裡面是 rank[i - 1] 
      for (; s[i + k] == s[sa[rank[i] - 1] + k]; k++);
      height[rank[i]] = k; // height 裡面是 rank 
    }
  }
  
  void buildst() {
    Log[1] = 0;
    for (int i = 2; i < N; i++) Log[i] = Log[i >> 1] + 1;
    for (int i = 1; i <= n; i++) st[i][0] = height[i];
    for (int j = 1; j < LOG; j++) {
      for (int i = 1; i + (1 << (j - 1)) <= n; i++) {
        st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
      }
    }
  }
  
  int queryst(int l, int r) {
    if (l > r) swap(l, r);
    ++l; //***
    int k = Log[r - l + 1];
    return min(st[l][k], st[r - (1 << k) + 1][k]);
  }
  
  int querylcp(int la, int ra, int lb, int rb) {
    if (rank[la] == rank[lb]) return min(ra - la + 1, rb - lb + 1);
    return min(min(ra - la + 1, rb - lb + 1), queryst(rank[la], rank[lb]));
  }
  
  //return substringa <= substringb
  bool cmpsubstring(int la, int ra, int lb, int rb) {
    int lcp = querylcp(la, ra, lb, rb);
    if (ra - la + 1 == lcp) return 1;
    if (rb - lb + 1 == lcp) return 0;
    return s[la + lcp] < s[lb + lcp];
  }
  
  pi findkth(ll k) {
    int pos = lower_bound(rank_pre + 1, rank_pre + n + 1, k) - rank_pre;
    return pi(sa[pos], sa[pos] + height[pos] + k - rank_pre[pos - 1] - 1); 
  }
} Sa;

int k, len;
char c[N];

bool check(pi cur) {
  int last = len, tot = 0;
  for (int i = len; i >= 1; i--) {
    if (!Sa.cmpsubstring(i, last, cur.first, cur.second)) {
      if (++tot > k) return 0;
      last = i;
      if (!Sa.cmpsubstring(i, last, cur.first, cur.second)) return 0;
    }
  }
  return ++tot <= k;
}

int main() {
#ifdef dream_maker
  freopen("input.txt", "r", stdin);
#endif
  scanf("%d", &k);
  scanf("%s", c + 1);
  len = strlen(c + 1);
  Sa.init(len, c);
  Sa.buildsa();
  Sa.buildrank();
  Sa.buildheight();
  Sa.buildrank_pre();
  Sa.buildst();
  ll l = 1, r = Sa.rank_pre[len];
  pi ans(1, 1);
  while (l <= r) {
    ll mid = (l + r) >> 1;
    pi cur = Sa.findkth(mid);
    if (check(cur)) {
      ans = cur, r = mid - 1;
    } else l = mid + 1;
  }
  for (int i = ans.first; i <= ans.second; i++) putchar(c[i]);
  return 0;
}