QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#274517 | #7876. Cyclic Substrings | _whb# | AC ✓ | 236ms | 481776kb | C++20 | 2.5kb | 2023-12-03 16:41:43 | 2023-12-03 16:41:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std ;
const int N = 2e5 + 5, M = 1e6 + 5, mod = 998244353 ;
using ll = long long ;
struct PAM_node
{
int fail,len ;
int ch[10] ;//可用map或hash表存边
int cnt1, cnt2 ;
};
struct PAM_node NIL={0} ;
struct PAM
{
vector<PAM_node> tr ;
int tot=0,last=0 ; // tot是节点个数 last是最后一次插入在PAM中的位置
string s ; // s中储存已经插入的串 , s的下标应从0开始
int getfail(int x,int i)
{
while( i-tr[x].len-1<0 || s[i-tr[x].len-1]!=s[i] ) x=tr[x].fail ;
return x ;
}
public:
explicit PAM()
{
tr.resize(2,NIL) ;
tot=1 ;
tr[0].fail=1 ; tr[1].fail=0 ;
tr[0].len=0 ; tr[1].len=-1 ; // 0为偶回文串根节点,1为奇回文串根节点
}
void insert1(char str,int i) // str是插入字符,i是插入字符对应的下标
{
s+=str ;
int pos=getfail(last,i) ;
if(!tr[pos].ch[s[i]-'0'])
{
tr.push_back(NIL) ;
tr[++tot].fail=tr[getfail(tr[pos].fail,i)].ch[s[i]-'0'] ;
tr[pos].ch[s[i]-'0']=tot ;
tr[tot].len=tr[pos].len+2 ;
}
last=tr[pos].ch[s[i]-'0'] ;
tr[last].cnt1 ++ ;
tr[last].cnt2 ++ ;
}
void insert2(char str,int i) // str是插入字符,i是插入字符对应的下标
{
s+=str ;
int pos=getfail(last,i) ;
if(!tr[pos].ch[s[i]-'0'])
{
tr.push_back(NIL) ;
tr[++tot].fail=tr[getfail(tr[pos].fail,i)].ch[s[i]-'0'] ;
tr[pos].ch[s[i]-'0']=tot ;
tr[tot].len=tr[pos].len+2 ;
}
last=tr[pos].ch[s[i]-'0'] ;
tr[last].cnt2 ++ ;
}
void toposort()
{
for(int i = tot; i >= 2; -- i)
{
tr[tr[i].fail].cnt1 += tr[i].cnt1 ;
tr[tr[i].fail].cnt2 += tr[i].cnt2 ;
}
}
};
int main()
{
ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
int n ; string s ; cin >> n >> s ;
PAM pam ;
// cout << "YES" << '\n' ;
for(int i = 0; i < n; ++ i) pam.insert1(s[i], i) ;
for(int i = 0; i < n; ++ i) pam.insert2(s[i], i + n) ;
pam.toposort() ;
ll ans = 0 ;
for(int i = 2; i <= pam.tot; ++ i)
if(pam.tr[i].len <= n)
{
ll len = pam.tr[i].len, cnt = pam.tr[i].cnt2 - pam.tr[i].cnt1 ;
ans = (ans + len * cnt % mod * cnt % mod) % mod ;
}
cout << ans << '\n' ;
return 0 ;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3432kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3444kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3484kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3484kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3432kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 59ms
memory: 122084kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 184ms
memory: 479000kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 143ms
memory: 243908kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 189ms
memory: 478632kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 188ms
memory: 481776kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 199ms
memory: 479888kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 236ms
memory: 479108kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 133ms
memory: 243876kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 52ms
memory: 71776kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 175ms
memory: 480256kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 199ms
memory: 479600kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed