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;
}
posted @ 2018-12-05 12:33  Dream_maker_yk  阅读(253)  评论(0编辑  收藏  举报