[HEOI2016/TJOI2016]字符串

嘟嘟嘟


今天复习一下SAM。


lcp固然不好做,干脆直接翻过来变成后缀。首先答案一定满足单调性,所以我们二分lcp的长度\(mid\),然后判断\(s[d \ldots d + mid - 1]\)是否在\(s[b \ldots a]\)(别忘了整个串是反过来的)中出现即可。
怎么判断是否出现呢?其实就是判断这个子串的endpos是否在\(s[b + mid - 1 \ldots a]\)中,因此我们要求出SAM上的每一个节点的endpos集合,这就要用到线段树合并了。
需要注意的是,并不是直接在\(d\)在SAM上的节点的线段树开始找,需要一直往上跳祖先,直到满足这个节点的len最小,且仍\(\geqslant mid\)。因为这样endpos集合的元素就会更多,找到的概率就更大。


线段树合并没有垃圾回收,不过出题人比较良心,不卡。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const int N = 20;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

char s[maxn];
int n, m, len;

struct Tree
{
  int ls, rs, sum;
}t[maxn * N * N];
int root[maxn << 1], cur[maxn << 1], tcnt = 0;
In void update(int& now, int l, int r, int id)
{
  if(!now) now = ++tcnt;
  if(l == r) {++t[now].sum; return;}
  int mid = (l + r) >> 1;
  if(id <= mid) update(t[now].ls, l, mid, id);
  else update(t[now].rs, mid + 1, r, id);
  t[now].sum = t[t[now].ls].sum + t[t[now].rs].sum;
}
In int merge(int x, int y, int l, int r)
{
  if(!x || !y) return x | y;
  if(l == r) {t[x].sum += t[y].sum; return x;}
  int mid = (l + r) >> 1, z = ++tcnt; 
  t[z].ls = merge(t[x].ls, t[y].ls, l, mid);
  t[z].rs = merge(t[x].rs, t[y].rs, mid + 1, r);
  t[z].sum = t[t[z].ls].sum + t[t[z].rs].sum;
  return z;
}
In int query(int now, int L, int R, int l, int r)
{
  if(!now) return 0;
  if(L == l && R == r) return t[now].sum;
  int mid = (l + r) >> 1;
  if(R <= mid) return query(t[now].ls, L, R, l, mid);
  else if(L > mid) return query(t[now].rs, L, R, mid + 1, r);
  else return query(t[now].ls, L, mid, l, mid) + query(t[now].rs, mid + 1, R, mid + 1, r);
}

struct Sam
{
  int las, cnt;
  int tra[maxn << 1][27], len[maxn << 1], link[maxn << 1];
  In void init() {link[las = cnt = 0] = -1;}
  In void insert(int c)
  {
    int now = ++cnt, p = las;
    len[now] = len[las] + 1;
    while(~p && !tra[p][c]) tra[p][c] = now, p = link[p];
    if(p == -1) link[now] = 0;
    else
      {
	int q = tra[p][c];
	if(len[q] == len[p] + 1) link[now] = q;
	else
	  {
	    int clo = ++cnt;
	    memcpy(tra[clo], tra[q], sizeof(tra[q]));
	    len[clo] = len[p] + 1;
	    link[clo] = link[q], link[q] = link[now] = clo;
	    while(~p && tra[p][c] == q) tra[p][c] = clo, p = link[p];
	  }
      }
    las = now;
  }
  int buc[maxn << 1], pos[maxn << 1];
  In void solve()
  {
    for(int i = 1; i <= cnt; ++i) ++buc[len[i]];
    for(int i = 1; i <= cnt; ++i) buc[i] += buc[i - 1];
    for(int i = 1; i <= cnt; ++i) pos[buc[len[i]]--] = i;
    for(int i = cnt; i; --i)
      {
	int now = pos[i], fa = link[now];
	root[fa] = merge(root[fa], root[now], 0, n - 1);
      }
  }
}S;

int fa[N + 2][maxn << 1];
In bool judge(int len, int x, int L, int R)
{
  for(int i = 20; i >= 0; --i)
    if(fa[i][x] && S.len[fa[i][x]] >= len) x = fa[i][x];
  return query(root[x], L + len - 1, R, 0, n - 1);
}

int main()
{
  //freopen("ha.in", "r", stdin);
  //freopen("ha.out", "w", stdout);
  n = read(), m = read();
  scanf("%s", s);
  len = strlen(s); S.init();
  reverse(s, s + len);
  for(int i = 0; i < n; ++i)
    {
      S.insert(s[i] - 'a'); cur[i] = S.las;
      update(root[cur[i]], 0, n - 1, i);
    }
  S.solve();
  for(int i = 1; i <= S.cnt; ++i) fa[0][i] = S.link[i];
  for(int j = 1; j <= N; ++j)
    for(int i = 1; i <= S.cnt; ++i) fa[j][i] = fa[j - 1][fa[j - 1][i]];
  for(int i = 1; i <= m; ++i)
    {
      int a = n - read(), b = n - read(), c = n - read(), d = n - read();
      int L = 0, R = min(a - b + 1, c - d + 1);
      while(L < R)
	{
	  int mid = (L + R + 1) >> 1;
	  if(judge(mid, cur[c], b, a)) L = mid;
	  else R = mid - 1;
	}
      write(L), enter;
    }
  return 0;
}
posted @ 2019-04-03 14:41  mrclr  阅读(226)  评论(0编辑  收藏  举报