跳到正文

剪贴板

剪贴板 nqx6e5eh

原文
剪贴板信息

剪贴板信息

收录
更新

Code for P6656

C++
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;

namespace standard_utilities {
  template <typename Ty>
  inline Ty max(const Ty &a, const Ty &b) {
    return a > b ? a : b;
  }

  template <typename Ty>
  inline Ty min(const Ty &a, const Ty &b) {
    return a < b ? a : b;
  }

  template <typename Ty>
  inline void check_max(Ty &a, const Ty &b) {
    if (b > a) a = b;
  }

  template <typename Ty>
  inline void check_min(Ty &a, const Ty &b) {
    if (b < a) a = b;
  }

  template <typename Ty>
  inline void swap(Ty &a, Ty &b) {
    Ty c = a;
    a = b, b = c;
  }
} // namespace standard_utilities

using namespace standard_utilities;

namespace Fast_IO {
  const int MaxBuff = 1 << 15;
  const int MaxOut = 1 << 24;

  char b[MaxBuff], *S = b, *T = b;
  char buff[MaxOut], *iter = buff;

  inline void flush() {
    fwrite(buff, 1, iter - buff, stdout), iter = buff;
  }

  inline char get_char() {
    return (S == T && (T = (S = b) + fread(b, 1, MaxBuff, stdin), S == T) ? 0 : *S++);
  }

  inline void put_char(char c) {
    if (iter - buff >= 1 << 24 - 1) flush();
    *iter++ = c;
  }
  
  struct Fast_IO_template {
    ~Fast_IO_template() { flush(); }

    inline Fast_IO_template &operator>>(char &c) {
      c = get_char();
      return *this;
    }

    inline Fast_IO_template &operator<<(char c) {
      put_char(c);
      return *this;
    }

    inline Fast_IO_template &operator>>(char *s) {
      char *iter = s;
      while (*iter = get_char(), *iter == ' ' || *iter == '\n' || *iter == '\r');
      while (*++iter = get_char(), *iter && *iter != ' ' && *iter != '\n' && *iter != '\r');
      *iter = 0;
      return *this;
    }

    inline Fast_IO_template &operator<<(int x) {
      static int stack[110];
      int O = 0;
      if (!x) put_char('0');
      else {
        if (x < 0) x = -x, put_char('-');
        for (; x; x /= 10) stack[++O] = x % 10;
        for (; O; put_char('0' + stack[O--]));
      }
      return *this;
    }
  };
} // namespace Fast_IO

Fast_IO::Fast_IO_template io;

const int N = 1e6 + 10;

struct my_tuple {
  int x, y, z;

  inline bool operator < (const my_tuple &b) const {
    if (x != b.x)
      return x < b.x;
    if (y != b.y)
      return y < b.y;
    return z < b.z;
  }

  inline int operator[](const int b) {
    if (b == 0)
      return x;
    if (b == 1)
      return y;
    if (b == 2)
      return z;
  }

  inline bool operator == (const my_tuple &b) { 
    return x == b.x && y == b.y && z == b.z; 
  }

  inline bool operator != (const my_tuple &b) { 
    return x != b.x || y != b.y || z != b.z; 
  }
};

struct my_pair {
  int x, y;

  inline int &operator[](const int b) { return b == 0 ? x : y; }
};

namespace SAIS {
  #define alloc(x, type, len) type *x = new type[len];

  void induced_sort(int n, int m, int *s, bool *T, int *sa, int *cnt) {
    static int lptr[N], rptr[N];
    int tmp;
    lptr[0] = rptr[0] = 0;
    for (int i = 1; i <= m; ++i) lptr[i] = cnt[i - 1], rptr[i] = cnt[i] - 1;
    for (int i = 0; i <= n; ++i) sa[i] > 0 && !T[tmp = sa[i] - 1] ? sa[lptr[s[tmp]]++] = tmp : 0;
    for (int i = n; i >= 0; --i) sa[i] > 0 &&  T[tmp = sa[i] - 1] ? sa[rptr[s[tmp]]--] = tmp : 0;
  }

  bool compare(int *x, int *y, int n) {
    while (n--) if (*x++ != *y++) return 1;
    return 0;
  }

  void sais(int n, int m, int *s, int *sa) {
    --n;
    alloc(T, bool, n + 10);
    alloc(cnt, int, m + 10);
    alloc(lms, int, n + 10);
    alloc(tmp, int, n + 10);
    for (int i = n; i >= 0; --i) {
      T[i] = (i == n || s[i] < s[i + 1] || s[i] == s[i + 1] && T[i + 1]);
      ++cnt[s[i]];
    }
    tmp[0] = 0;
    for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1], tmp[i] = cnt[i] - 1;
    int LMS = 0;
    for (int i = 1; i <= n; ++i) T[i] && !T[i - 1] ? lms[LMS++] = i : 0;
    memset(sa, -1, (n + 1) << 2);
    for (int i = 0; i < LMS; ++i) sa[tmp[s[lms[i]]]--] = lms[i];
    induced_sort(n, m, s, T, sa, cnt);
    alloc(len, int, n + 10);
    lms[LMS] = n + 1;
    for (int i = 0; i < LMS; ++i) len[lms[i]] = lms[i + 1] - lms[i] + 1;
    alloc(lab, int, n + 10);
    int m0 = 0;
    memset(lab, -1, (n + 1) << 2);
    for (int i = 1, p = -1, q = -1; i <= n; ++i)
      if ((q = sa[i]) > 0 && T[q] && !T[q - 1]) {
        if (p == -1 || len[p] != len[q] || compare(s + p, s + q, len[p])) ++m0;
        lab[q] = m0; p = q;
      }
    lab[n] = 0;
    alloc(s0, int, LMS + 10);
    alloc(sa0, int, LMS + 10);
    for (int i = 0, p = 0; i <= n; ++i) lab[i] >= 0 ? s0[p++] = lab[i] : 0;
    if (m0 + 1 == LMS) for (int i = 0; i < LMS; ++i) sa0[s0[i]] = i;
    else sais(LMS, m0, s0, sa0);
    tmp[0] = 0;
    for (int i = 1; i <= m; ++i) tmp[i] = cnt[i] - 1;
    memset(sa, -1, (n + 1) << 2);
    for (int i = LMS - 1; i >= 0; --i) sa[tmp[s[lms[sa0[i]]]]--] = lms[sa0[i]];
    induced_sort(n, m, s, T, sa, cnt);
  }
} // namespace SAIS

inline int log2n(int n) { return 32 - __builtin_clz(n - 1); }

namespace RMQ_table {
  const int N = 2e6 + 20;
  const int D_max = 21;
  const int L = 20;
  const int inf = 1 << 30;

  int stack[L + 1];

  template <typename T, int MAXN>
  class ST {
   private:
    int f[N / L][D_max], M[N / L], a[N + L], n, q, D;

    struct node {
      int state[L + 1], *a;

      int Qmin(int x, int y) { return a[x + __builtin_ctz(state[y] >> x)]; }

      void init(int *_a) {
        int top = 0;
        a = _a;
        for (int i = 1; i <= L; ++i) {
          state[i] = state[i - 1];
          while (top && a[i] <= a[stack[top]])
            state[i] -= 1 << stack[top], --top;
          stack[++top] = i;
          state[i] += 1 << i;
        }
      }
    } c[N / L];

   public:
    void build(int _n, int *_a) {
      n = _n;
      for (int i = 1; i <= _n; ++i) a[i] = _a[i];
      D = int(log2(n)) + 1;
      int nn = n / L;
      M[0] = -1;
      for (int i = 1; i <= nn; ++i) {
        f[i][0] = inf;
        for (int j = 1; j <= L; ++j)
          f[i][0] = min(f[i][0], a[(i - 1) * L + j]);
      }
      for (int i = 1; i <= nn; ++i)
        M[i] = !(i & (i - 1)) ? M[i - 1] + 1 : M[i - 1];
      for (int j = 1; j <= D; ++j)
        for (int i = 1; i <= nn - (1 << j) + 1; ++i)
          f[i][j] = min(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
      for (int i = 1; i <= nn + 1; ++i)
        c[i].init(a + (i - 1) * L);
    }

    inline int Qmin_ST(int x, int y) {
      int z = M[y - x + 1];
      return min(f[x][z], f[y - (1 << z) + 1][z]);
    }

    inline int rmq(int x, int y) {
      ++x, ++y;
      int xx = (x - 1) / L + 1, yy = (y - 1) / L + 1, res;
      if (xx + 1 <= yy - 1)res = Qmin_ST(xx + 1, yy - 1); else res = inf;
      if (xx == yy)res = min(res, c[xx].Qmin(x - (xx - 1) * L, y - (yy - 1) * L));
      else res = min(res, c[xx].Qmin(x - (xx - 1) * L, L)), res = min(res, c[yy].Qmin(1, y - (yy - 1) * L));
      return res;
    }
  };
} // namespace RMQ_table

using RMQ_table::ST;

template <int N, template <typename, int> class RMQ_T>
class SA {
 public:
  RMQ_T<int, N - 1> rmq;
  int n, sa[N], rk[N], height[N];

  void build(int _n, char *str) {
    n = _n;
    alloc(s, int, n + 10);
    for (int i = 0; i < n; ++i) s[i] = str[i];
    SAIS::sais(n + 1, 26, s, sa);
    for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
    for (int k = 0, i = 0, j; i < n; height[rk[i++]] = k)
    for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++k);
    rmq.build(n, height);
  }

  int lcp(int i, int j) {
    //printf("lcp(%d,%d)\n", i, j);
    if (i == j) return n - i;
    if (j == n) return 0;
    i = rk[i], j = rk[j];
    if (i > j) std::swap(i, j); 
    return rmq.rmq(i, j - 1);
  }
};

template <int N, template <typename, int> class RMQ_T>
class ISA {
 public:
  RMQ_T<int, N - 1> rmq;
  int n, sa[N], rk[N], height[N];

  void build(int _n, char *str) {
    n = _n;
    alloc(s, int, n + 10);
    for (int i = 0; i < n; ++i) s[i] = str[n - i - 1];
    SAIS::sais(n + 1, 26, s, sa);
    for (int i = 1; i <= n; ++i) rk[sa[i]] = i;
    for (int k = 0, i = 0, j; i < n; height[rk[i++]] = k)
    for (k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++k);
    rmq.build(n, height);
    //for (int i = 2; i <= n + 1; ++i) printf("%d ", height[i]); printf("\n");
    //for (int i = 0; i < n; ++i) printf("%d ", rk[i]); printf("\n");
  }

  int lcs(int i, int j) {
    //++i, ++j;
    if (i <= 0 || j <= 0) return 0;
    if (i == j) return i;
    i = rk[n - i], j = rk[n - j];
    if (i > j) std::swap(i, j);
    //printf("rmq(%d,%d)", i, j);
    return rmq.rmq(i, j - 1);
  }
};

int n, nsv[N], nbv[N];
char s[N];
SA<N, ST> sa;
ISA<N, ST> isa;
std::vector<my_tuple> runs;

int get_lcs(int i, int j) {
  int low = 0, high = min(i, j);
  while (low < high) {
    int middle = low + high + 1 >> 1;
    if (sa.lcp(i - middle, j - middle) >= middle)
      low = middle;
    else
      high = middle - 1;
  }
  //printf("lcs(%d,%d) : %d %d\n", i, j, isa.lcs(i, j), low);
  return low;
}

void add_run(int i, int j) {
  int length = j - i;
  int lcs = isa.lcs(i, j);
  if (lcs < length) {
    int lcp = sa.lcp(i, j);
    if (lcs + lcp >= length) runs.push_back((my_tuple){i - lcs + 1, j + lcp, length});
  }
}

std::vector<int> a[N], a2[N], a3[N];
int b[N], c[N];

void my_sort_and_unique() {
  int m = runs.size();
  for (int i = 0; i < m; ++i) a[runs[i].z].push_back(i);
  for (int i = 1, tmp = 0; i <= n; ++i)
    for (int j = 0; j < a[i].size(); ++j) b[tmp++] = a[i][j];
  for (int i = 0; i < m; ++i) a2[runs[b[i]].y].push_back(b[i]);
  for (int i = 1, tmp = 0; i <= n; ++i)
    for (int j = 0; j < a2[i].size(); ++j) b[tmp++] = a2[i][j];
  for (int i = 0; i < m; ++i) a3[runs[b[i]].x].push_back(b[i]);
  for (int i = 1, tmp = 0; i <= n; ++i)
    for (int j = 0; j < a3[i].size(); ++j) b[tmp++] = a3[i][j];
  int tmp = 0; c[0] = b[0];
  for (int i = 1; i < m; ++i)
    if (runs[b[i - 1]] != runs[b[i]]) c[++tmp] = b[i];
  io << tmp + 1 << '\n';
  for (int i = 0; i <= tmp; ++i)
    io << runs[c[i]].x << ' ' << runs[c[i]].y << ' ' << runs[c[i]].z << '\n';
}

int main() {
  io >> s; n = strlen(s);
  for (int i = 0; i < n; ++i) s[i] = s[i] - 'a' + 1;
  sa.build(n, s);
  isa.build(n, s);
  for (int i = n; i--;) {
    for (int &j = nsv[i] = i + 1; j < n && sa.rk[i] < sa.rk[j];) j = nsv[j];
    for (int &j = nbv[i] = i + 1; j < n && sa.rk[i] > sa.rk[j];) j = nbv[j];
    add_run(i, nsv[i]);
    if (nsv[i] != nbv[i]) add_run(i, nbv[i]);
  }
  std::sort(runs.begin(), runs.end());
  runs.erase(std::unique(runs.begin(), runs.end()), runs.end());
  io << (int)runs.size() << '\n';
  for (auto a : runs) io << a.x << ' ' << a.y << ' ' << a.z << '\n';
  return 0;
}