提交记录 20568


用户 题目 状态 得分 用时 内存 语言 代码长度
adpitacor 1006. 【模板题】后缀排序 Accepted 100 40.758 ms 3288 KB C++14 1.90 KB
提交时间 评测时间
2023-11-10 14:37:33 2023-11-10 14:37:39
#include<cassert>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<utility>
using std::cin; using std::cout;
#define N_ 1000010
int n;
char s[N_];
int sa[N_];
int cnt[N_],aux[2][N_];
auto *rnk=aux[0],*tmp=aux[1];
int height[N_];
bool cmp(int x,int y,int w){
    //printf("cmp(%d,%d,%d): (%d,%d) v.s. (%d,%d)\n"
    //,x,y,w,rnk[x],rnk[x+w],rnk[y],rnk[y+w]);
    return rnk[x]==rnk[y]&&((x+w<=n?rnk[x+w]:-1)==(y+w<=n?rnk[y+w]:-1));
}
/*
void show(){
    printf("sa: ");
    for(int i=1;i<=n;++i)printf(" %d",sa[i]);
    putchar('\n');
    printf("rnk:");
    for(int i=1;i<=n;++i)printf(" %d",rnk[i]);
    putchar('\n');
}
*/
void suffix_sort(){
    int i=0,j=0,k=0;
    for(i=0;(++i)<=n;)++cnt[s[i]];
    while((++j)^128)cnt[j]+=cnt[j-1];
    while(--i)rnk[i]=cnt[s[i]]--;
    while((++i)<=n)sa[rnk[i]]=i;
    while(--i)rnk[i]=cnt[s[i]]+1;
    i=n+1;
    for(int len=1;(j=n-len)>0;len<<=1){
        k=0;
        while((--i)^j)tmp[++k]=i;
        for(i=0;(++i)<=n;)if(sa[i]>len)tmp[++k]=sa[i]-len;
        while(--i)cnt[i]=0;
        for(++k;--k;)++cnt[rnk[tmp[k]]];
        for(++i;(++i)<=n;)cnt[i]+=cnt[i-1];
        while(--i)sa[cnt[rnk[tmp[i]]]--]=tmp[i];
        for(++k;(++i)^n||(tmp[sa[i]]=k,++i,false);){
            tmp[sa[i]]=(i^n&&cmp(sa[i],sa[i+1],len)?k:k++);
        }
        std::copy(tmp+1,tmp+n+1,rnk+1);
        //std::swap(rnk,tmp);
    }
}
void calc_height(){
    int i,j,k;
    for(i=1,k=0;i<=n;++i){
        if (rnk[i]==1)continue;
        if(k)--k;
        j=sa[rnk[i]-1];
        while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k])++k;
        height[rnk[i]]=k;
    }
}
int main(){
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    //scanf("%s",s+1);
    cin>>(s+1);
    n=strlen(s+1);
    suffix_sort();
    calc_height();
    for(int i=1;i<=n;++i)cout<<sa[i]<<' ';//printf("%d ",sa[i]);
    cout<<'\n';
    for(int i=2;i<=n;++i)cout<<height[i]<<' ';
    if(n==1)cout<<'\n';
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #145.73 us80 KBAcceptedScore: 100

Subtask #1 Testcase #242.81 us84 KBAcceptedScore: 0

Subtask #1 Testcase #341.27 us84 KBAcceptedScore: 0

Subtask #1 Testcase #442.99 us84 KBAcceptedScore: 0

Subtask #1 Testcase #544.16 us84 KBAcceptedScore: 0

Subtask #1 Testcase #644.28 us84 KBAcceptedScore: 0

Subtask #1 Testcase #740.467 ms2 MB + 860 KBAcceptedScore: 0

Subtask #1 Testcase #840.758 ms3 MB + 24 KBAcceptedScore: 0

Subtask #1 Testcase #940.401 ms2 MB + 912 KBAcceptedScore: 0

Subtask #1 Testcase #1024.414 ms1 MB + 936 KBAcceptedScore: 0

Subtask #1 Testcase #1124.014 ms1 MB + 976 KBAcceptedScore: 0

Subtask #1 Testcase #1230.922 ms3 MB + 216 KBAcceptedScore: 0

Subtask #1 Testcase #1330.6 ms3 MB + 184 KBAcceptedScore: 0

Subtask #1 Testcase #1439.21 ms2 MB + 940 KBAcceptedScore: 0

Subtask #1 Testcase #1538.861 ms2 MB + 956 KBAcceptedScore: 0

Subtask #1 Testcase #1630.832 ms3 MB + 216 KBAcceptedScore: 0

Subtask #1 Testcase #1730.748 ms3 MB + 216 KBAcceptedScore: 0

Subtask #1 Testcase #1830.903 ms3 MB + 216 KBAcceptedScore: 0


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