1 条题解

  • 1
    @ 2025-1-18 19:49:26
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 5;
    int a[N], n, rk[N], sa[N], cnt[N];
    struct Seq 
    {
        int num[2], id;
    } s[N], cp[N];
    char c[N];
    int main() 
    {
        scanf("%s", c);
        int n = strlen(c);
    
        for (int i = 1; i <= n; i++)
            rk[i] = a[i] = c[i - 1];
    
        sort(rk + 1, rk + n + 1);
        int len = 0;
    
        for (int i = 1; i <= n; i++)
            if (rk[i] != rk[i - 1])
                rk[++len] = rk[i];
    
        for (int i = 1; i <= n; i++)
            a[i] = lower_bound(rk + 1, rk + len + 1, a[i]) - rk;
    
        for (int i = 1; i <= n; i++)
            rk[i] = a[i];
    
        for (int i = 1; i <= n; i <<= 1) 
        {
            for (int j = 1; j <= n; j++) 
            {
                s[j].num[0] = rk[j];
    
                if (j + i <= n) s[j].num[1] = rk[j + i];
                else s[j].num[1] = 0;
    
                s[j].id = j;
            }
    
            for (int k = 1; k >= 0; k--) 
            {
                for (int j = 0; j <= n; j++)
                    cnt[j] = 0;
    
                for (int j = 1; j <= n; j++)
                    cnt[s[j].num[k]]++;
    
                for (int j = 1; j <= n; j++)
                    cnt[j] += cnt[j - 1];
    
                for (int j = n; j >= 1; j--)
                    cp[cnt[s[j].num[k]]--] = s[j];
    
                for (int j = 1; j <= n; j++)
                    s[j] = cp[j];
            }
    
            int tot = 0;
    
            for (int j = 1; j <= n; j++) 
            {
                if (s[j].num[0] != s[j - 1].num[0] || s[j].num[1] != s[j - 1].num[1])
                    tot++;
    
                rk[s[j].id] = tot;
            }
        }
    
        for (int i = 1; i <= n; i++)
            sa[rk[i]] = i;
    
        for (int i = 1; i <= n; i++)
            printf("%d ", sa[i]);
    
        return 0;
    }
    
    

    信息

    ID
    5542
    时间
    3000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者