QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#606279 | #7876. Cyclic Substrings | lonelywolf | AC ✓ | 358ms | 720968kb | C++20 | 3.5kb | 2024-10-03 00:12:59 | 2024-10-03 00:13:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define int long long
using ll = long long;
struct PAM {
static constexpr int ALPHABET_SIZE = 10;
struct Node {
int len;
int fail;
int dep; // 以这个节点结尾的回文子串的数量(回文fail树的深度)
int cnt = 0; // 同样的回文结构出现次数
array<int, ALPHABET_SIZE> next;
// int mask = 0;用了多少种字母
Node() : len{}, fail{}, dep{}, next{} {}
};
vector<Node> t;
vector<int> idpos; // idpos表示字符串字符位置到后缀自动机节点编号
int last;
string s;
PAM() {
init();
}
void init() {
t.assign(2, Node());
t[0].len = -1; // 0:奇根
last = 1; // 1:偶根
s.clear();
idpos.assign(1, 0);
}
int newNode() {
t.emplace_back();
return t.size() - 1;
}
bool add(char c, char offset = '0') {
int pos = s.size();
s += c;
int ch = c - offset;
int cur = last, curlen = 0;
while (true) {
curlen = t[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) {
break;
}
cur = t[cur].fail;
} // 找到在哪个节点后面建新点
if (t[cur].next[ch]) {
last = t[cur].next[ch];
idpos.push_back(last);
t[last].cnt += 1;
return false;
}
int num = newNode();
last = num;
idpos.push_back(last);
t[num].len = t[cur].len + 2;
// 在这里加入题目需要维护的值
// t[num].mask = t[cur].mask;
// t[num].mask |= 1 << ch;
t[cur].next[ch] = num;
if (t[num].len == 1) { // 如果为单字符,指向偶根
t[num].fail = 1;
t[num].dep = 1;
t[num].cnt = 1;
return true;
}
while (true) { // 为新节点找fail,从父亲的fail开始找
cur = t[cur].fail;
curlen = t[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]) {
t[num].fail = t[cur].next[ch];
break;
}
}
t[num].cnt = 1;
t[num].dep = 1 + t[t[num].fail].dep;
return true;
}
int tot = 0;
void work(string tt) {
for (auto x : tt) add(x);
tot = t.size() - 1;
for (int i = tot; i >= 0; i--) {
int fa = t[i].fail;
t[fa].cnt += t[i].cnt;
}
}
int fail(int p) {
return t[p].fail;
}
int len(int p) {
return t[p].len;
}
int size() {
return t.size();
}
int cnt(int p) {
return t[p].cnt;
}
};
constexpr int mod = 998244353;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
string t = s + s;
PAM ps, pt;
ps.work(s), pt.work(t);
int ans = 0;
for (int i = 1; i < ps.size(); i++) {
ans = (ans + 1LL * (pt.cnt(i) - ps.cnt(i)) * (pt.cnt(i) - ps.cnt(i)) % mod * ps.len(i) % mod) % mod;
}
for (int i = ps.size(); i < pt.size(); i++) {
if (pt.len(i) <= n) {
ans = (ans + 1LL * pt.cnt(i) * pt.cnt(i) % mod * pt.len(i) % 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: 3876kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3844kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3592kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 96ms
memory: 243600kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 284ms
memory: 719608kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: 0
Accepted
time: 349ms
memory: 456360kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
224009870
result:
ok 1 number(s): "224009870"
Test #11:
score: 0
Accepted
time: 282ms
memory: 720968kb
input:
3000000 8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...
output:
51985943
result:
ok 1 number(s): "51985943"
Test #12:
score: 0
Accepted
time: 240ms
memory: 720856kb
input:
3000000 1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...
output:
355676465
result:
ok 1 number(s): "355676465"
Test #13:
score: 0
Accepted
time: 358ms
memory: 709180kb
input:
3000000 7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...
output:
788510374
result:
ok 1 number(s): "788510374"
Test #14:
score: 0
Accepted
time: 342ms
memory: 710104kb
input:
3000000 5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...
output:
691884476
result:
ok 1 number(s): "691884476"
Test #15:
score: 0
Accepted
time: 224ms
memory: 490120kb
input:
3000000 0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...
output:
701050848
result:
ok 1 number(s): "701050848"
Test #16:
score: 0
Accepted
time: 130ms
memory: 208152kb
input:
3000000 2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...
output:
486861605
result:
ok 1 number(s): "486861605"
Test #17:
score: 0
Accepted
time: 292ms
memory: 689332kb
input:
3000000 4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...
output:
450625621
result:
ok 1 number(s): "450625621"
Test #18:
score: 0
Accepted
time: 316ms
memory: 683332kb
input:
3000000 1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...
output:
649551870
result:
ok 1 number(s): "649551870"
Extra Test:
score: 0
Extra Test Passed