1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  | // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <bits/stdc++.h>
using namespace std;
static vector<int> buildSuffixArray(const string& s) {
    int n = (int)s.size();
    vector<int> sa(n), rankCurrent(n), rankPrev(n), counting(max(256, n) + 1, 0);
    // Initial ranking by single characters (shifted to 1..256, sentinel 0 reserved)
    for (int i = 0; i < n; i++) rankCurrent[i] = (unsigned char)s[i] + 1;
    // Initial SA by counting sort on first key (characters)
    fill(counting.begin(), counting.end(), 0);
    for (int i = 0; i < n; i++) counting[rankCurrent[i]]++;
    for (int i = 1; i < (int)counting.size(); i++) counting[i] += counting[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--counting[rankCurrent[i]]] = i;
    // Doubling
    int maxRank = max(256, n);
    for (int k = 1; k < n; k <<= 1) {
        // Sort by second key via induced ordering
        int p = 0;
        for (int i = n - k; i < n; i++) rankPrev[p++] = i;               // suffixes with empty second part
        for (int i = 0; i < n; i++) if (sa[i] >= k) rankPrev[p++] = sa[i] - k;
        // Counting sort by first key (rankCurrent)
        fill(counting.begin(), counting.end(), 0);
        for (int i = 0; i < n; i++) counting[rankCurrent[rankPrev[i]]]++;
        for (int i = 1; i <= maxRank; i++) counting[i] += counting[i - 1];
        for (int i = n - 1; i >= 0; i--) sa[--counting[rankCurrent[rankPrev[i]]]] = rankPrev[i];
        // Re-rank
        rankPrev.swap(rankCurrent);
        rankCurrent[sa[0]] = 1;
        p = 1;
        for (int i = 1; i < n; i++) {
            int a = sa[i - 1], b = sa[i];
            int a1 = rankPrev[a], b1 = rankPrev[b];
            int a2 = (a + k < n) ? rankPrev[a + k] : 0;
            int b2 = (b + k < n) ? rankPrev[b + k] : 0;
            if (a1 == b1 && a2 == b2) {
                rankCurrent[b] = p;
            } else {
                rankCurrent[b] = ++p;
            }
        }
        maxRank = p;
        if (p == n) break;
    }
    return sa;
}
static vector<int> buildLCP(const string& s, const vector<int>& sa) {
    int n = (int)s.size();
    vector<int> rankInSa(n), lcp(n, 0);
    for (int i = 0; i < n; i++) rankInSa[sa[i]] = i;
    int h = 0;
    for (int i = 0; i < n; i++) {
        int r = rankInSa[i];
        if (r == 0) { h = 0; continue; }
        int j = sa[r - 1];
        while (i + h < n && j + h < n && s[i + h] == s[j + h]) h++;
        lcp[r] = h;
        if (h > 0) h--;
    }
    return lcp;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    if (!(cin >> s)) return 0;
    int n = (int)s.size();
    vector<int> sa = buildSuffixArray(s);
    vector<int> lcp = buildLCP(s, sa);
    // Output SA (1-based)
    for (int i = 0; i < n; i++) {
        if (i) cout << ' ';
        cout << (sa[i] + 1);
    }
    cout << '\n';
    // Output LCP with leading 'x'
    cout << 'x';
    for (int i = 1; i < n; i++) {
        cout << ' ' << lcp[i];
    }
    cout << '\n';
    return 0;
}
  |