BZOJ3230: 相似子串【后缀数组】

Description

img

Input

输入第1行,包含3个整数N,Q。Q代表询问组数。
第2行是字符串S。
接下来Q行,每行两个整数i和j。(1≤i≤j)。

Output

输出共Q行,每行一个数表示每组询问的答案。如果不存在第i个子串或第j个子串,则输出-1。

Sample Input

5 3
ababa
3 5
5 9
8 10

Sample Output

18
16
-1

HINT

样例解释

第1组询问:两个子串是“aba”,“ababa”。f = 32 + 32 = 18。

第2组询问:两个子串是“ababa”,“baba”。f = 02 + 42 = 16。

第3组询问:不存在第10个子串。输出-1。

数据范围

N≤100000,Q≤100000,字符串只由小写字母'a'~'z'组成


直接正串反串建立SA然后求出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 sum[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 buildsum() {
    for (int i = 1; i <= n; i++) sum[i] = sum[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); 
      for (; s[i + k] == s[sa[rank[i] - 1] + k]; k++);
      height[rank[i]] = k;
    }
  }
  
  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) return n - sa[l] + 1;
    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) {
    return min(min(ra - la + 1, rb - lb + 1), queryst(rank[la], rank[lb]));
  }
  
  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(sum + 1, sum + n + 1, k) - sum;
    return pi(sa[pos], sa[pos] + height[pos] + k - sum[pos - 1] - 1); 
  }
  
  ll getrank(int l, int r) {
    int pos = rank[l], len = r - l + 1;
    for (int i = LOG - 1; i >= 0; i--) {
      if (pos > (1 << i) && st[pos - (1 << i) + 1][i] >= len) {
        pos -= (1 << i);
      } 
    }
    return sum[pos - 1] + len - height[pos];
  }
  
  void build(int len, char *c) {
    init(len, c);
    buildsa();
    buildrank();
    buildheight();
    buildsum();
    buildst();
  } 
} Sa, revSa;

char s[N], revs[N];
int len, q;

int main() {
  scanf("%d %d", &len, &q);
  scanf("%s", s + 1);
  for (int i = 1; i <= len; i++) revs[i] = s[len - i + 1];
  Sa.build(len, s);
  revSa.build(len, revs);
  while (q--) {
    ll x, y; scanf("%lld %lld", &x, &y); 
    if (Sa.sum[len] < max(x, y)) {
      printf("-1\n");
      continue;
    }
    pi curx = Sa.findkth(x), cury = Sa.findkth(y);
    int a = Sa.querylcp(curx.first, curx.second, cury.first, cury.second);
    int b = revSa.querylcp(len - curx.second + 1, len - curx.first + 1, len - cury.second + 1, len - cury.first + 1);
    printf("%lld\n", 1ll * a * a + 1ll * b * b);
  }
  return 0;
}
posted @ 2018-12-05 13:05  Dream_maker_yk  阅读(246)  评论(0编辑  收藏  举报