QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#282259 | #7876. Cyclic Substrings | ucup-team2443 | AC ✓ | 292ms | 571444kb | C++20 | 2.1kb | 2023-12-11 17:00:32 | 2023-12-11 17:00:32 |
Judging History
answer
#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<<1],e[N<<1],ne[N<<1],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 dfs(int x){
for(int i=h[x];~i;i=ne[i]){
int j=e[i];
dfs(j);
ans[x]+=ans[j];
if(ans[x]>=mod) ans[x]-=mod;
}
}
int main()
{
memset(h,-1,sizeof h);
cin>>n;
scanf("%s",s+1);
// for(int i=1;i<=n;i++) s[i]='2';
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;
// }
dfs(1);
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: 5ms
memory: 61008kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 7ms
memory: 61140kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 0ms
memory: 61052kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 4ms
memory: 61112kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 58956kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 61128kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 7ms
memory: 61196kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 52ms
memory: 229200kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 118ms
memory: 571444kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 93ms
memory: 266760kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 101ms
memory: 515028kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 130ms
memory: 496964kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 115ms
memory: 437580kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 103ms
memory: 447804kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 133ms
memory: 265324kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 56ms
memory: 127824kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 282ms
memory: 405404kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 292ms
memory: 392968kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed