提交记录 17712


用户 题目 状态 得分 用时 内存 语言 代码长度
WA_King 1006. 【模板题】后缀排序 Accepted 100 36.184 ms 4464 KB C++11 3.09 KB
提交时间 评测时间
2022-05-20 14:33:36 2022-05-20 14:33:40
#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef WA_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

using ll  = long long;
using ull = unsigned long long;
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++)
#define per(i,a,b) for(int i=int(a);i>=(int)(b);i--)
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
const int mod  = 1e9+7;
const int inf  = 0x3f3f3f3f;
const int maxn = 1e5+10;
const int maxbit=19;
struct SuffixArray
{
    // sa: 排名为i的后缀位置
    // rank:位置为i的排名
    int sa[maxn], rank[maxn], ws[maxn], wv[maxn], wa[maxn], wb[maxn], height[maxn], st[maxbit][maxn], N;
    bool cmp(int *r, int a, int b, int l){return r[a]==r[b] and r[a+l]==r[b+l];}
    void build(int *r, int n, int m)
    {
        N=n;
        n++;
        int i, j, k=0, p, *x=wa, *y=wb, *t;
        for(i=0;i<m;i++)ws[i]=0;
        for(i=0;i<n;i++)ws[x[i]=r[i]]++;
        for(i=1;i<m;i++)ws[i]+=ws[i-1];
        for(i=n-1;i>=0;i--)sa[--ws[x[i]]]=i;
        for(p=j=1;p<n;j<<=1,m=p)
        {
            for(p=0,i=n-j;i<n;i++)y[p++]=i;
            for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
            for(i=0;i<n;i++)wv[i]=x[y[i]];
            for(i=0;i<m;i++)ws[i]=0;
            for(i=0;i<n;i++)ws[wv[i]]++;
            for(i=1;i<m;i++)ws[i]+=ws[i-1];
            for(i=n-1;i>=0;i--)sa[--ws[wv[i]]]=y[i];
            for(t=x,x=y,y=t,p=1,i=1,x[sa[0]]=0;i<n;i++)
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
        
        for(i=0;i<n;i++)rank[sa[i]]=i;
        
        for(i=0;i<n-1;height[rank[i++]]=k)
            for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
    }
    void build_st()     //st表
    {
        int i, k;
        for(i=1;i<=N;i++)st[0][i]=height[i];
        for(k=1;k<=maxbit;k++)
            for(i=1;i+(1<<k)-1<=N;i++)
                st[k][i]=min(st[k-1][i],st[k-1][i+(1<<k-1)]);
    }
    int lcp(int x, int y)   //最长公共前缀
    {
        int l=rank[x], r=rank[y];
        if(l>r)swap(l,r);
        if(l==r)return N-sa[l];
        int t=log2(r-l);
        return min(st[t][l+1],st[t][r-(1<<t)+1]);
    }
}SA;
int ch[maxn];
int main() {
#ifndef WA_DEBUG
    ios::sync_with_stdio(false);cin.tie(nullptr);
#endif
    string s;
    cin>>s;
    int n=s.size();
    rep(i,0,n-1) {
        ch[i]=s[i];
    }
    ch[n]=0;
    SA.build(ch,n,200);
    rep(i,1,n) {
        cout<<SA.sa[i]+1<<' ';
    }
    cout<<'\n';
    rep(i,2,n) {
        cout<<SA.height[i]<<' ';
    }
    cout<<'\n';
    return 0;
}


CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #143.71 us96 KBAcceptedScore: 0

Subtask #1 Testcase #247.48 us96 KBAcceptedScore: 100

Subtask #1 Testcase #346.84 us96 KBAcceptedScore: 0

Subtask #1 Testcase #448.99 us108 KBAcceptedScore: 0

Subtask #1 Testcase #547.25 us108 KBAcceptedScore: 0

Subtask #1 Testcase #648.26 us108 KBAcceptedScore: 0

Subtask #1 Testcase #720.397 ms4 MB + 64 KBAcceptedScore: 0

Subtask #1 Testcase #836.184 ms4 MB + 284 KBAcceptedScore: 0

Subtask #1 Testcase #922.388 ms4 MB + 148 KBAcceptedScore: 0

Subtask #1 Testcase #1014.459 ms2 MB + 816 KBAcceptedScore: 0

Subtask #1 Testcase #1115.256 ms2 MB + 856 KBAcceptedScore: 0

Subtask #1 Testcase #1227.723 ms4 MB + 348 KBAcceptedScore: 0

Subtask #1 Testcase #1326.484 ms4 MB + 368 KBAcceptedScore: 0

Subtask #1 Testcase #1423.734 ms4 MB + 176 KBAcceptedScore: 0

Subtask #1 Testcase #1523.443 ms4 MB + 184 KBAcceptedScore: 0

Subtask #1 Testcase #1627.833 ms4 MB + 348 KBAcceptedScore: 0

Subtask #1 Testcase #1727.929 ms4 MB + 348 KBAcceptedScore: 0

Subtask #1 Testcase #1828.326 ms4 MB + 348 KBAcceptedScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-22 18:52:07 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠