其他分享
首页 > 其他分享> > [CTSC2012]熟悉的文章 [广义后缀自动机,二分+单调队列优化dp]

[CTSC2012]熟悉的文章 [广义后缀自动机,二分+单调队列优化dp]

作者:互联网

题意:

给定多个主串,每次将询问串拆分成多个连续子串,如果该子串在主串中出现,那么这段是合法的,要求一个最大的 \(L\) 使得合法的长度 \(\geq\) 询问串长度的 \(90\%\)

我们发现 \(L\) 是可以二分的,我们再考虑怎么 \(check\) 这个是否合法,对于一个 \(L\)。

我们设 \(mx_i\) 为 \(i\) 节点结尾最长能匹配的长度。

\(F_i = \min\{F_{i-1},F_j+i-j\} \ \ i - mx_i \leq j \leq i - L\)

我们发现 \(i - mx_i\)是非降的, \(i-L\) 是严格上升的,所以可以用单调队列 + \(dp\) 来判定可行性。

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
#define Tp template
using pii = pair<int, int>;
#define fir first
#define sec second
Tp<class T> void cmax(T& x, const T& y) {
  if (x < y) x = y;
}
Tp<class T> void cmin(T& x, const T& y) {
  if (x > y) x = y;
}
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
Tp<class T> void sort(vector<T>& v) { sort(all(v)); }
Tp<class T> void reverse(vector<T>& v) { reverse(all(v)); }
Tp<class T> void unique(vector<T>& v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
const int SZ = 1 << 23 | 233;
struct FILEIN {
  char qwq[SZ], *S = qwq, *T = qwq, ch;
#ifdef __WIN64
#define GETC getchar
#else
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
#endif
  FILEIN& operator>>(char& c) {
    while (isspace(c = GETC()))
      ;
    return *this;
  }
  FILEIN& operator>>(string& s) {
    while (isspace(ch = GETC()))
      ;
    s = ch;
    while (!isspace(ch = GETC())) s += ch;
    return *this;
  }
  Tp<class T> void read(T& x) {
    bool sign = 0;
    while ((ch = GETC()) < 48) sign ^= (ch == 45);
    x = (ch ^ 48);
    while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48);
    x = sign ? -x : x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 1 << 22;
  char quq[SZ], ST[233];
  int sz, O;
  ~FILEOUT() { flush(); }
  void flush() {
    fwrite(quq, 1, O, stdout);
    fflush(stdout);
    O = 0;
  }
  FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
  FILEOUT& operator<<(string str) {
    if (O > LIMIT) flush();
    for (char c : str) quq[O++] = c;
    return *this;
  }
  Tp<class T> void write(T x) {
    if (O > LIMIT) flush();
    if (x < 0) {
      quq[O++] = 45;
      x = -x;
    }
    do {
      ST[++sz] = x % 10 ^ 48;
      x /= 10;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;

const int maxn = 2e6 + 62;
char s[maxn];

struct SAM {
  int cnt, las, ch[maxn][26], len[maxn], fa[maxn];

  SAM() { cnt = las = 1; }

  void ins(int c) {
    int p = las, np = las = ++cnt;
    len[np] = len[p] + 1;
    for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np;
    if (!p) {
      fa[np] = 1;
    } else {
      int q = ch[p][c];
      if (len[q] == len[p] + 1) {
        fa[np] = q;
      } else {
        int nq = ++cnt;
        memcpy(ch[nq], ch[q], sizeof(ch[q]));
        fa[nq] = fa[q], fa[q] = fa[np] = nq, len[nq] = len[p] + 1;
        for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq;
      }
    }
  }

  int mx[maxn];

  void find() {
    int L = strlen(s + 1), p = 1, mxl = 0;
    for (int i = 1; i <= L; i++) {
      int c = s[i] & 1;
      for (; p && !ch[p][c];) p = fa[p], mxl = len[p];
      if (p)
        p = ch[p][c], ++mxl;
      else
        p = 1, mxl = 0;
      mx[i] = mxl;
    }
  }

  int f[maxn], q[maxn];
  bool chk(int mid) {
    int L = strlen(s + 1), h, t;
    h = t = 0, q[++h] = 0;
    for (int i = 1; i <= mid - 1; i++) f[i] = 0;
    for (int i = mid; i <= L; i++) {
      f[i] = f[i - 1];
      while (h <= t && f[q[t]] - q[t] < f[i - mid] - (i - mid)) --t;
      q[++t] = i - mid;
      while (h <= t && q[h] < i - mx[i]) ++h;
      if (h <= t) cmax(f[i], f[q[h]] + i - q[h]);
    }
    return f[L] * 10 >= L * 9;
  }
} sam;

signed main() {
  // code begin.
  int _, n;
  scanf("%d %d" , & _ , & n) ;
  while (n--) {
    scanf("%s", s + 1);
    sam.las = 1;
    int len = strlen(s + 1);
    for (int i = 1; i <= len; i++) {
      sam.ins(s[i] & 1);
    }
  }
  while (_--) {
    scanf("%s", s + 1), sam.find();
    int l = 1, r = strlen(s + 1), ans = 0;
    while (l <= r) {
      int mid = l + r >> 1;
      if (sam.chk(mid)) {
        l = mid + 1;
        ans = mid;
      } else {
        r = mid - 1;
      }
    }
    out << ans << '\n';
  }
  return 0;
  // code end.
}

标签:ch,后缀,void,Tp,int,while,CTSC2012,dp,define
来源: https://www.cnblogs.com/Isaunoya/p/12510271.html