QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#377950 | #8223. 字符串游戏 | zhouhuanyi | 15 | 1301ms | 368084kb | C++14 | 3.6kb | 2024-04-05 21:00:56 | 2024-04-05 21:00:56 |
Judging History
answer
#include<iostream>
#include<cstdio>
#define N 1000001
using namespace std;
const int inf=(int)(1e9);
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
string s;
int n,length,top,rt[N+1],tmp[N+1],lg[N+1],dp[N+1],ps[N+1],scnt[N+1],sa[N+1],h[N+1],rk[N+1],cnt[N+1],ST[N+1][21];
char c[N+1];
bool vis[N+1];
struct reads
{
int l,r,d;
};
reads dque[N+1];
struct seg
{
struct node
{
int ls,rs,data;
};
node tree[30*N+1];
int length;
void push_up(int k)
{
tree[k].data=tree[tree[k].ls].data+tree[tree[k].rs].data;
return;
}
int add(int k,int L,int R,int x,int d)
{
int nw=++length;
tree[nw]=tree[k];
if (L==R)
{
tree[nw].data+=d;
return nw;
}
int mid=(L+R)>>1;
if (x<=mid) tree[nw].ls=add(tree[nw].ls,L,mid,x,d);
else tree[nw].rs=add(tree[nw].rs,mid+1,R,x,d);
push_up(nw);
return nw;
}
int query(int k,int L,int R,int l,int r)
{
if (!k) return 0;
if (L==l&&R==r) return tree[k].data;
int mid=(L+R)>>1;
if (r<=mid) return query(tree[k].ls,L,mid,l,r);
else if (l>=mid+1) return query(tree[k].rs,mid+1,R,l,r);
else return query(tree[k].ls,L,mid,l,mid)+query(tree[k].rs,mid+1,R,mid+1,r);
}
};
seg T;
void build_SA()
{
for (int i=1;i<=n;++i) ps[i]=++cnt[c[i]-'a'+1];
for (int i=1;i<=26;++i) scnt[i]=scnt[i-1]+(cnt[i]>0),cnt[i]+=cnt[i-1];
for (int i=1;i<=n;++i) sa[cnt[c[i]-'a']+ps[i]]=i,rk[i]=scnt[c[i]-'a'+1];
length=scnt[26];
for (int i=1;length<n;i<<=1)
{
for (int j=1;j<=length;++j) cnt[j]=0;
for (int j=n-i+1;j<=n;++j) ps[j]=++cnt[rk[j]];
for (int j=1;j<=n;++j)
if (sa[j]>=i+1)
ps[sa[j]-i]=++cnt[rk[sa[j]-i]];
for (int j=1;j<=length;++j) cnt[j]+=cnt[j-1];
for (int j=1;j<=n;++j) sa[cnt[rk[j]-1]+ps[j]]=j;
length=0;
for (int j=1;j<=n;++j)
{
if (j==1||rk[sa[j]]!=rk[sa[j-1]]||(sa[j]+i<=(n<<1)?rk[sa[j]+i]:0)!=(sa[j-1]+i<=(n<<1)?rk[sa[j-1]+i]:0)) ++length;
tmp[sa[j]]=length;
}
for (int j=1;j<=n;++j) rk[j]=tmp[j];
for (int j=1;j<=length;++j) vis[j]=0;
}
for (int i=1;i<=n;++i)
{
h[rk[i]]=max(h[rk[i-1]]-1,0);
if (rk[i]!=1)
{
while (i+h[rk[i]]<=n&&c[i+h[rk[i]]]==c[sa[rk[i]-1]+h[rk[i]]]) h[rk[i]]++;
}
}
return;
}
int query(int l,int r)
{
int lw=lg[r-l+1];
return min(ST[l][lw],ST[r-(1<<lw)+1][lw]);
}
int calc(int l,int r)
{
int d=r-l+1,sl=rk[l],sr=rk[l];
for (int i=lg[n];i>=0;--i)
if (sr+(1<<i)<=n&&query(rk[l]+1,sr+(1<<i))>=d)
sr+=(1<<i);
for (int i=lg[n];i>=0;--i)
if (sl-(1<<i)>=1&&query(sl-(1<<i)+1,rk[l])>=d)
sl-=(1<<i);
return T.query(rt[l],1,n,sl,sr)-dp[r+1];
}
int main()
{
int ps;
for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
cin>>s,n=s.length();
for (int i=1;i<=n;++i) c[i]=s[i-1];
build_SA();
for (int i=n;i>=1;--i) rt[i]=T.add(rt[i+1],1,n,rk[i],1);
for (int i=1;i<=n;++i) ST[i][0]=h[i];
for (int i=1;i<=lg[n];++i)
for (int j=1;j+(1<<i)-1<=n;++j)
ST[j][i]=min(ST[j][i-1],ST[j+(1<<(i-1))][i-1]);
for (int i=n;i>=1;--i)
{
if (i!=n)
{
if (dque[top].l==i+1) top--;
else dque[top].r--;
}
while (top&&calc(dque[top].l,i)>calc(dque[top].l,dque[top].d)) top--;
if (top&&calc(dque[top].r,i)>calc(dque[top].r,dque[top].d))
{
ps=dque[top].r;
for (int j=lg[dque[top].r-dque[top].l+1];j>=0;--j)
if (ps-(1<<j)>=dque[top].l&&calc(ps-(1<<j),i)>calc(ps-(1<<j),dque[top].d))
ps-=(1<<j);
dque[top].r=ps-1;
}
ps=dque[top].r+1;
if (ps<=i) dque[++top]=(reads){ps,i,i};
dp[i]=calc(i,dque[top].d);
}
printf("%d\n",dp[1]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 18ms
memory: 23748kb
input:
abbabbbbabbaaaaabbbaabaaaaaabbbaabbabababaabababaababaaaaaabaaabaaabbbbababaaabbbabababbbabbbaabaaababbbaabaabbaaabbababaaaaabbbaabaaabbaabaabaaabaaabaababaabbbbbbbbabbbbbabbbaabababbaababbbbbabbbbaaabbaabaabaaabbabbaaabbaaababaabbbaabbbaaaabbababbabaababababaaaabbaaaabaabaaaababaaabababbbaaabababbb...
output:
1281
result:
ok single line: '1281'
Test #2:
score: 0
Accepted
time: 18ms
memory: 21336kb
input:
aaaabababbabbbbbabbbbbbbbbbbabbbbbabbbaaabbbbaababaabbbbbbbbbbbbbabaabbaaabaababaabaaabbabbbabbbabaabbaabaababbbbbbaaabbbbaaabaabbbabbbabbabaabbbbabbbaabaaaabbbbbabaaaabaabbabaaabbaabbabbaaabbabaaababbababababaabaaaabaabaabaaabaabbbabbbbbaababbabbbabbbbababaaabbabbaaaabbbaaabaabbbaaaaaababaababbbbba...
output:
1483
result:
ok single line: '1483'
Test #3:
score: 0
Accepted
time: 13ms
memory: 22668kb
input:
bbaaababbbabbbaabbbbbbababbbabbbbbbbbaabbbbbbbbbbaaabbbbbbbbbbbbbbbabbabbbabaababbbabbbbabbabbabbbbabbbbbbbbbaaababbaaabbbaabbabbbbbbbbbbbbbbbbbbbbabbbbbbababababbbbbbbbbabababaabbbbbbabbbbaabbababbaaababbbbabbbbabbbabbbbbabbbbbabbbbbbbbbbabbaabbbabababbbbaababbbababbbbabbabbaabbbbbbabbbbbababbbbaba...
output:
2310
result:
ok single line: '2310'
Test #4:
score: 0
Accepted
time: 17ms
memory: 22200kb
input:
abbbbbbbbababbbbbbbbbabbbbbbbbabbbabbbbbaaabbbbbabbbbababbbabbbbbbaabbabbbbbaabbbbabbabbbbbbbabbbbaaabbbbbbaabbbbbbbbbbbbbbbbbbbbbabbbabbbabbbbbbbabbbbbbbabbbabbbbbbbbabbbabbbbbbbbbbbbbbbbbaababbbbbbbbbbabbbbbbbbbbbbbababbbbbbbabbbbbbababbbabbbbbaabbbbbbbbbaabbabbbbaabbbabbbbbbbbbbbbbabbbbbbbbbbbbbb...
output:
1
result:
ok single line: '1'
Test #5:
score: 0
Accepted
time: 14ms
memory: 19504kb
input:
bbbabbbbbbbbbbbbbbbbbbbbabbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbabbbabbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbabbbbbbbbbabbbbabbabbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbabbbbbbbbbbbabbbbbbbbbbbbbbbbbbbabbbbbbabbabbbbbbaabbbabbbbbbbbbbbbbbbbabbbbbabbbabbbbbbbabbbababbabbbbba...
output:
3680
result:
ok single line: '3680'
Test #6:
score: 0
Accepted
time: 15ms
memory: 19104kb
input:
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbb...
output:
2454
result:
ok single line: '2454'
Test #7:
score: 0
Accepted
time: 11ms
memory: 19300kb
input:
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
2702
result:
ok single line: '2702'
Test #8:
score: 0
Accepted
time: 16ms
memory: 22184kb
input:
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
2499
result:
ok single line: '2499'
Test #9:
score: 0
Accepted
time: 14ms
memory: 21272kb
input:
amahaaajakalaiacabararasafaxasagamajaqagaqaeauauayagasadazavaeagabaaajalajavalatauapacaearaeagafawaaapawadaiaaapalahanavazasayamaiafagabahaxanacaaagaoadasakayagawaiaharanamaraiaramazaoagajataoazasalavatafaoalacaqavanafatajawauayakaoaqawawayasatasazagafacayasasawaqayadasaiazaxamaaaragataxawazadalaxak...
output:
2475
result:
ok single line: '2475'
Test #10:
score: 0
Accepted
time: 17ms
memory: 20868kb
input:
afabagadaeaeacaeaeafagafagababaaagacabaaagadacafadabaaaaacadaaagafacagadaeacacacabadagadaaabafadafadadaeafaaagadafabaeaaafaeaeaaaeabaaabaaaeafagadaaaaacaeaaaaacabaeacadagafafacacabaeaaagagaeaaadagabadaaaaagafagaaabaaaaafacafacadaaagabaaafadagacaaafafadadadacadadaaacagabacaaabacaeadabacagaeagagafafag...
output:
2807
result:
ok single line: '2807'
Subtask #2:
score: 0
Time Limit Exceeded
Test #11:
score: 0
Time Limit Exceeded
input:
aaaaaaabbaaabbbbabbabbbababaabaaabaabbaaababaaaaaabbbbbaabbbabbbaabbabaababaababaabbbababbbabaaaaaaaaabaaaaabbbabbbbbabaaabaaaababbabbbbbbaabbbbbaababbbbababbbbbbbbbbbaaabbabbaababbaaaabbabaaabbaaabbbbaaababbbbbabbaababbaaababaaabbabaabbbbabbaaabbababbaabbaaaaaaaaaabababbbaaaababbabbbabbababbabbabbb...
output:
result:
Subtask #3:
score: 5
Accepted
Test #16:
score: 5
Accepted
time: 1301ms
memory: 368084kb
input:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
493827
result:
ok single line: '493827'
Subtask #4:
score: 0
Time Limit Exceeded
Test #17:
score: 0
Time Limit Exceeded
input:
bbbbbbbbbaabbbbbabaaabaabaaabaaaaabaaaabbbbbbbaaaabaabbbababbaaaabbabababbbabbbbabbabaabaaabbabbaaababaaabaaaabababbbbbbbaabbaaabbabbbbbabbbaaaaababbbaaabaabababbabbabababbabbabaaaaabaaaaaabbbabaabbbaaaabbabababaaababaabbbbbbbaaabbbabaabaaaabaabaababbaaaabababaabbbaaaababaaababbbbbbabbbbbbaabbabbaaa...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #2:
0%