提交记录 14290


用户 题目 状态 得分 用时 内存 语言 代码长度
lyfer 1006. 【模板题】后缀排序 Accepted 100 58.791 ms 10564 KB C++11 1.54 KB
提交时间 评测时间
2020-09-19 22:24:37 2020-09-19 22:24:44
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int Maxn=1e6+5;
char s[Maxn];
int n,m,x[Maxn],y[Maxn],c[Maxn],sa[Maxn];
int height[Maxn],rk[Maxn];

void SA()
{	
    for(int i=1;i<=n;i++){x[i]=s[i];++c[x[i]];}
    for(int i=2;i<=m;i++)c[i]+=c[i-1];
    for(int i=n;i>=1;i--)sa[c[x[i]]--]=i;
    for(int k=1;k<=n;k=k<<1)
    {	
        int num=0;
        for (int i=n-k+1;i<=n;++i) y[++num]=i;
        for(int i=1;i<=n;i++)
            if(sa[i]>k)y[++num]=sa[i]-k;
        for(int i=1;i<=m;i++)c[i]=0;
        for(int i=1;i<=n;i++)c[x[i]]++;
        for(int i=2;i<=m;i++)c[i]+=c[i-1];
        for(int i=n;i>=1;i--){sa[c[x[y[i]]]--]=y[i];y[i]=0;}
        swap(x,y);
        num=1;x[sa[1]]=1;
        for(int i=2;i<=n;i++)
        {	
            if(y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k])x[sa[i]]=num;
            else x[sa[i]]=++num;
        }
        if(num==n)break;
        m=num;
    }
    for(int i=1;i<=n;i++)
        printf("%d ",sa[i]);
    printf("\n");
}

void LCP()
{	
    int k=0; //用k代表h[i]
    for(int i=1;i<=n;i++)rk[sa[i]]=i; //初始化rk[i]
    for(int i=1;i<=n;i++)//这里其实是枚举rk[i]
    {	
        if(rk[i]==1)continue; //height[1]=0
        if(k)k--; //h[i]>=h[i-1]-1,更新k然后一位位枚举
        int j=sa[rk[i]-1];//前一位字符串
        while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k])k++;//一位位枚举
        height[rk[i]]=k;//h[i]=height[rk[i]]
    }
    for(int i = 2; i <= n; i++)
        printf("%d ",height[i]);
    printf("\n");
}

int main()
{	
    scanf("%s",s+1);
    n=strlen(s+1);
    m=122;
    SA();
    LCP(); 
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #11.565 ms7 MB + 696 KBAcceptedScore: 0

Subtask #1 Testcase #21.574 ms7 MB + 700 KBAcceptedScore: 100

Subtask #1 Testcase #31.571 ms7 MB + 700 KBAcceptedScore: 0

Subtask #1 Testcase #42.928 ms7 MB + 700 KBAcceptedScore: 0

Subtask #1 Testcase #52.928 ms7 MB + 700 KBAcceptedScore: 0

Subtask #1 Testcase #62.924 ms7 MB + 700 KBAcceptedScore: 0

Subtask #1 Testcase #733.072 ms10 MB + 16 KBAcceptedScore: 0

Subtask #1 Testcase #858.791 ms10 MB + 240 KBAcceptedScore: 0

Subtask #1 Testcase #936.831 ms10 MB + 108 KBAcceptedScore: 0

Subtask #1 Testcase #1025.314 ms9 MB + 260 KBAcceptedScore: 0

Subtask #1 Testcase #1126.892 ms9 MB + 304 KBAcceptedScore: 0

Subtask #1 Testcase #1250.18 ms10 MB + 304 KBAcceptedScore: 0

Subtask #1 Testcase #1347.63 ms10 MB + 324 KBAcceptedScore: 0

Subtask #1 Testcase #1439.729 ms10 MB + 136 KBAcceptedScore: 0

Subtask #1 Testcase #1539.377 ms10 MB + 136 KBAcceptedScore: 0

Subtask #1 Testcase #1648.87 ms10 MB + 304 KBAcceptedScore: 0

Subtask #1 Testcase #1748.625 ms10 MB + 304 KBAcceptedScore: 0

Subtask #1 Testcase #1848.889 ms10 MB + 304 KBAcceptedScore: 0


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