QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303672 | #5065. Beautiful String | erduolong | WA | 125ms | 4280kb | C++20 | 1.9kb | 2024-01-12 21:02:20 | 2024-01-12 21:02:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=5005;
int T,n,bu[N],lg[N];
char s[N];
namespace SA{
int sa[N],rk[N],id[N],bu[N],p,st[17][N];
void ss(){
memset(sa,0,sizeof sa);
memset(rk,0,sizeof rk);
memset(id,0,sizeof id);
memset(bu,0,sizeof bu);
memset(st,0,sizeof st),p=0;
for(int i=1;i<=n;++i) ++bu[s[i]];
for(int i=1;i<='9';++i) bu[i]+=bu[i-1];
for(int i=n;i;--i) sa[bu[s[i]]--]=i;
for(int i=1;i<=n;++i) rk[sa[i]]=s[sa[i]]==s[sa[i-1]]?p:++p;
for(int i=1;p<n;i*=2){
for(int j=1;j<=p;++j) bu[j]=0;
for(int j=1;j<=i;++j) id[j]=n-i+j;
for(int j=1,k=i;j<=n;++j) if(sa[j]>i) id[++k]=sa[j]-i;
for(int j=1;j<=n;++j) ++bu[rk[j]];
for(int j=1;j<=p;++j) bu[j]+=bu[j-1];
for(int j=n;j;--j) sa[bu[rk[id[j]]]--]=id[j],id[j]=rk[j];
p=0;
for(int j=1;j<=n;++j) rk[sa[j]]=id[sa[j]]==id[sa[j-1]]&&id[sa[j]+i]==id[sa[j-1]+i]?p:++p;
}
for(int i=1,k=0;i<=n;++i){
k-=!!k;
if(sa[i]>1) while(s[i+k]==s[sa[rk[i]-1]+k]) ++k;
st[0][rk[i]]=k;
}
for(int i=1,p=2;p<=n;++i,p*=2)
for(int j=1;j+p-1<=n;++j)
st[i][j]=min(st[i-1][j],st[i-1][j+p/2]);
}
int lcp(int x,int y){
if(x==y) return n-x+1;
if((x=rk[x])>(y=rk[y])) swap(x,y);
int t=lg[y-(++x)+1];
return min(st[t][x],st[t][y-(1<<t)+1]);
}
}
using SA::lcp;
long long ans;
void solve(){
memset(s,0,sizeof s),scanf("%s",s+1),n=strlen(s+1),SA::ss();
for(int i=1;i<=n;++i){
for(int j=i+2;j<=n;++j)
++bu[min(j-i-1,lcp(i,j))];
for(int j=n;j;--j) bu[j-1]+=bu[j];
for(int j=1;j<i;++j)
if(lcp(j,i)>=i-j)
ans+=bu[i-j+1];
for(int j=0;j<=n;++j) bu[j]=0;
}
// for(int i=1;i<=n;++i,puts(""))
// for(int j=1;j<=n;++j)
// printf("%d ",lcp(i,j));
printf("%lld\n",ans),ans=0;
}
int main(){
//freopen("A.in","r",stdin);
//freopen("B2.out","w",stdout);
for(int i=1;i<N;++i) lg[i]=lg[i-1]+(2<<lg[i-1]==i);
scanf("%d",&T);
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4264kb
input:
2 114514 0000000
output:
1 3
result:
ok 2 number(s): "1 3"
Test #2:
score: -100
Wrong Answer
time: 125ms
memory: 4280kb
input:
11 79380 2483905227 37902399703986576270 39991723655814713848046032694137883815354365954917 5541883522667841718787744915558298207485830622522715211018760095628594390563630840464643840093757297 56530485554219245862513973750218715585679416120445975390556326891488719311495909340757506478400624741858999...
output:
0 0 0 1 4 18 105 103 977 1930 13623
result:
wrong answer 4th numbers differ - expected: '2', found: '1'