QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#521996 | #7876. Cyclic Substrings | ucup-team3474 | AC ✓ | 307ms | 479108kb | C++23 | 2.4kb | 2024-08-16 17:11:53 | 2024-08-16 17:11:53 |
Judging History
answer
#pragma GCC optimize("O3,unroll-loops")
//这行告诉GCC编译器使用O3优化级别和循环展开。O3是GCC提供的最高优化级别,它会尝试使用所有的程序优化策略。"unroll-loops"是一个特定的优化选项,它会尝试将循环展开以减少循环的开销。
#include<bits/stdc++.h>
using namespace std;
const int N=6e6+10,mod=998244353;
typedef long long ll;
typedef pair<ll,ll> PII;
char s[N];
int n;
int fail[N],len[N],num[N],idx;
int tr[N][10];
ll ans[N];
int h[N],e[N],ne[N],idx2;
int rd[N];
vector<int> topo;
int getfail(int x,int id){//对id新建fail指针
while(s[id-len[x]-1]!=s[id]) x=fail[x];//如果不对称,往前跳
return x;
}
void add(int a,int b){
e[idx2]=b,ne[idx2]=h[a],h[a]=idx2++;
}
void topsort(){
queue<int> q;
q.push(1);
while(q.size()){
int t=q.front();
q.pop();
topo.push_back(t);
for(int i=h[t];~i;i=ne[i]){
int j=e[i];
rd[j]--;
if(rd[j]==0) q.push(j);
}
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n;
scanf("%s",s+1);
for(int i=1;i<=n;i++) s[i+n]=s[i];
fail[0]=1,len[1]=-1;//初始化0,1号点
idx=1;
int p=0,cur=0;
for(int i=1;i<=n*2;i++){
p=getfail(cur,i);//求fail要在建立指针之前
if(!tr[p][s[i]-'0']){//没有的话(直接匹配成功了的话)建立新点
fail[++idx]=tr[getfail(fail[p],i)][s[i]-'0'];//找到“次长回文串”的位置,它的儿子就是最长回文串的位置
tr[p][s[i]-'0']=idx;//建立新点
len[idx]=len[p]+2;//维护以i结尾的最长回文串长度
num[idx]=num[fail[idx]]+1;//维护以i结尾的回文串个数(除了最长的那个别的都出现了)
}
cur=tr[p][s[i]-'0'];
if(i>n) ans[cur]++;
}
for(int i=0;i<=idx;i++){
if(i==1) continue;
add(fail[i],i);
rd[i]++;
}
topsort();
for(int i=topo.size()-1;i>=0;i--){
int t=topo[i];
ans[fail[t]]+=ans[t];
if(ans[fail[t]]>=mod) ans[fail[t]]-=mod;
}
ll res=0;
for(int i=0;i<=idx;i++){
if(i==1) continue;
if(len[i]<=n){
res+=len[i]*ans[i]%mod*ans[i]%mod;
if(res>=mod) res-=mod;
}
}
cout<<res<<endl;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 38468kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 0ms
memory: 40872kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 3ms
memory: 38464kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 38484kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 38504kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 40536kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 7ms
memory: 40636kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 39ms
memory: 193492kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 116ms
memory: 478472kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 102ms
memory: 255048kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 96ms
memory: 478608kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 100ms
memory: 479108kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 152ms
memory: 438048kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 96ms
memory: 452888kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 158ms
memory: 275472kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 59ms
memory: 111932kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 307ms
memory: 426600kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 307ms
memory: 417512kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed