QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#538328#7406. Longest Lyndon Prefixsolar_express#TL 1ms4584kbC++201.5kb2024-08-31 10:29:592024-08-31 10:29:59

Judging History

你现在查看的是最新测评结果

  • [2024-08-31 10:29:59]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:4584kb
  • [2024-08-31 10:29:59]
  • 提交

answer

#include<bits/stdc++.h>
#define N 100005
using namespace std;
int T,n;
char s[N];
int sz;
int rak[N],sa[N],tp[N],c[N];
void Rsort(){
    for(int i=0;i<=sz;++i) c[i]=0;
    for(int i=1;i<=n;++i) ++c[rak[i]];
    for(int i=1;i<=sz;++i) c[i]+=c[i-1];
    for(int i=n;i>=1;--i) sa[c[rak[tp[i]]]--]=tp[i];
}
void sa_build(){
    sz=127;
    for(int i=1;i<=n;++i)
        rak[i]=s[i],tp[i]=i;
    Rsort();
    for(int w=1,p=0;p<n;sz=p,w<<=1){
        assert(w<=n);
        p=0;
        for(int i=1;i<=w;++i)
            tp[++p]=n-w+i;
        for(int i=1;i<=n;++i)
            if(sa[i]>w)
                tp[++p]=sa[i]-w;
        Rsort();
        swap(tp,rak);
        rak[sa[1]]=p=1;
        for(int i=2;i<=n;++i)
            rak[sa[i]]=(tp[sa[i-1]]==tp[sa[i]]&&tp[sa[i-1]+w]==tp[sa[i]+w])?p:++p;
    }
}
int tr[N],ans[N];
void init(){
    for(int i=1;i<=n;++i) tr[i]=n+1;
}
void modify(int x,int v){
    for(int i=x;i<=n;i+=i&(-i)) tr[i]=min(tr[i],v);
}
int qry(int x){
    int res=n+1;
    for(int i=x;i;i-=i&(-i)) res=min(res,tr[i]);
    return res;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%s",&n,s+1);
        if(T<=100)sa_build();
        init();
        for(int i=n;i>=1;--i){
            ans[i]=qry(rak[i])-1;
            modify(rak[i],i);
        }
        for(int i=1;i<=n;++i)
            printf("%d ",ans[i]-i+1);
        puts("");
        for(int i=1;i<=n;++i) sa[i]=rak[i]=tp[i]=0;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4584kb

input:

3
3
aaa
3
aab
3
cba

output:

1 1 1 
3 2 1 
1 1 1 

result:

ok 9 numbers

Test #2:

score: -100
Time Limit Exceeded

input:

10000
10
ababbbbaaa
6
aabbaa
3
abb
9
bababbbbb
9
abbaaaaaa
8
ababbaab
7
abbbbbb
7
aaabaaa
2
ba
10
abaababbab
2
ab
1
a
1
a
5
ababa
6
aaabba
2
ba
4
abba
5
bbbba
9
aabbbbbaa
10
baaabaaaba
10
babbbbbbaa
9
babaaabba
1
b
6
abbbaa
7
aaaaaab
10
baaaaabaaa
9
bbbbabbba
3
bbb
8
abaababa
7
bbbbaba
5
ababb
1
b
7...

output:


result: