From 6cc4f140811df694659051fe46c5764f1819a8fb Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 6 Jun 2022 20:31:32 +0800 Subject: [PATCH] =?UTF-8?q?2715.=20=E5=90=8E=E7=BC=80=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AcWing/2715/2715.cpp | 86 +++++++++++++++++++++++++++++++++++++++++ AcWing/2715/data/12.in | 3 ++ AcWing/2715/data/12.out | 3 ++ 3 files changed, 92 insertions(+) create mode 100644 AcWing/2715/2715.cpp create mode 100644 AcWing/2715/data/12.in create mode 100644 AcWing/2715/data/12.out diff --git a/AcWing/2715/2715.cpp b/AcWing/2715/2715.cpp new file mode 100644 index 00000000..f1410a5c --- /dev/null +++ b/AcWing/2715/2715.cpp @@ -0,0 +1,86 @@ +#pragma GCC optimize("O3,unroll-loops") +#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") + +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n, m, cnt[N], id[N], px[N], sa[N], height[N]; +std::string s; +std::array rk, rk1; + +void get_sa() { + for (int i = 1; i <= n; i++) ++cnt[rk[i] = s[i]]; + for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; + for (int i = n; i; i--) sa[cnt[rk[i]]--] = i; + + for (int w = 1, p = 0, i;; w <<= 1, m = p) { + for (i = n, p = 0; i > n - w; i--) id[++p] = i; + for (int i = 1; i <= n; i++) { + if (sa[i] > w) id[++p] = sa[i] - w; + } + + std::fill(cnt, cnt + 1 + m, 0); + + for (int i = 1; i <= n; i++) ++cnt[px[i] = rk[id[i]]]; + for (int i = 1; i <= m; i++) cnt[i] += cnt[i - 1]; + for (int i = n; i; i--) sa[cnt[px[i]]--] = id[i]; + + rk1 = rk; + for (i = 1, p = 0; i <= n; i++) { + if (rk1[sa[i]] == rk1[sa[i - 1]] && rk1[sa[i] + w] == rk1[sa[i - 1] + w]) { + rk[sa[i]] = p; + } else { + rk[sa[i]] = ++p; + } + } + + if (p == n) { + for (int i = 1; i <= n; i++) sa[rk[i]] = i; + + break; + } + } +} + +void get_height() { + for (int i = 1, k = 0; i <= n; i++) { + if (!rk[i]) continue; + if (k) k--; + + while (s[i + k] == s[sa[rk[i] - 1] + k]) k++; + + height[rk[i]] = k; + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> s; + n = s.size(); + m = 300; + s = ' ' + s; + + get_sa(); + get_height(); + + for (int i = 1; i <= n; i++) { + cout << sa[i] << ' '; + } + cout << endl; + + for (int i = 1; i <= n; i++) { + cout << height[i] << ' '; + } + cout << endl; + + return 0; +} diff --git a/AcWing/2715/data/12.in b/AcWing/2715/data/12.in new file mode 100644 index 00000000..cb77d20f --- /dev/null +++ b/AcWing/2715/data/12.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e5955d1fcbe7b291bbed6a6c23628f3935659c63f3328bae0d8f52c8aea4cf51 +size 1000001 diff --git a/AcWing/2715/data/12.out b/AcWing/2715/data/12.out new file mode 100644 index 00000000..fc34af97 --- /dev/null +++ b/AcWing/2715/data/12.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5b6c4297827349180aa740507d559b82474305f7a3360995fbd2f582d4ddaa51 +size 13777788