QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#538331 | #7406. Longest Lyndon Prefix | solar_express# | TL | 1ms | 5924kb | C++20 | 1.5kb | 2024-08-31 10:31:14 | 2024-08-31 10:31:15 |
Judging History
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);
sa_build();
init();
for(int i=n;i>=1;--i){
assert(rak[i]>0);
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: 5924kb
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:
7 1 5 1 1 1 1 1 1 1 4 3 1 1 1 1 3 1 1 1 8 1 6 1 1 1 1 1 3 1 1 1 1 1 1 1 1 5 1 3 1 1 3 2 1 7 1 1 1 1 1 1 4 3 2 1 1 1 1 1 1 2 1 8 5 1 3 1 1 2 1 2 1 1 1 2 1 2 1 1 5 4 3 1 1 1 1 1 3 1 1 1 1 1 1 1 1 7 6 1 1 1 1 1 1 1 1 4 3 2 1 4 3 2 1 1 1 7 1 1 1 1 1 1 1 1 1 2 1 5 4 3 1 1 1 1 4 1 1...