From d32ddd13863f862a4da325198ca6f4c344041ba2 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 6 Jun 2022 16:47:10 +0800 Subject: [PATCH] =?UTF-8?q?#35.=20=E5=90=8E=E7=BC=80=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://uoj.ac/submission/557218 --- UniversalOJ/35/35.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 UniversalOJ/35/35.cpp diff --git a/UniversalOJ/35/35.cpp b/UniversalOJ/35/35.cpp new file mode 100644 index 00000000..9abb6e5e --- /dev/null +++ b/UniversalOJ/35/35.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n, sa[N], height[N]; +std::string s; +std::array rk, rk1; + +void get_sa() { + for (int i = 1; i <= n; i++) { + sa[i] = i; + rk[i] = s[i]; + } + + for (int w = 1; w < n; w <<= 1) { + std::sort(sa + 1, sa + 1 + n, [&](int a, int b) { + return rk[a] == rk[b] ? rk[a + w] < rk[b + w] : rk[a] < rk[b]; + }); + + rk1 = rk; + + for (int 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; + } + } + } +} + +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(); + s = ' ' + s; + + get_sa(); + get_height(); + + for (int i = 1; i <= n; i++) { + cout << sa[i] << ' '; + } + cout << endl; + + for (int i = 2; i <= n; i++) { + cout << height[i] << ' '; + } + cout << endl; + + return 0; +}