From 61cdf9c0cc014e49ece2e1b15e75d8143e974c61 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 3 Jun 2022 20:57:30 +0800 Subject: [PATCH] =?UTF-8?q?P3809=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=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://www.luogu.com.cn/record/76864570 --- Luogu/P3809/P3809.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Luogu/P3809/P3809.cpp diff --git a/Luogu/P3809/P3809.cpp b/Luogu/P3809/P3809.cpp new file mode 100644 index 00000000..7c46dc10 --- /dev/null +++ b/Luogu/P3809/P3809.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e6 + 5; + +int n; +std::string s; +std::array sa; +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.begin() + 1, sa.begin() + 1 + n, [&](auto x, auto y) { + return rk[x] == rk[y] ? rk[x + w] < rk[y + w] : rk[x] < rk[y]; + }); + + 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; + } + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> s; + n = s.size(); + s = ' ' + s; + + get_sa(); + + for (int i = 1; i <= n; i++) { + cout << sa[i] << ' '; + } + cout << endl; + + return 0; +}