QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#303385 | #7876. Cyclic Substrings | mendicillin2 | WA | 0ms | 3556kb | C++17 | 2.5kb | 2024-01-12 13:13:50 | 2024-01-12 13:13:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
using ll = int64_t;
template <class F> struct ycr { /// start-hash
F f;
template <class T> explicit ycr(T&& f_) : f(forward<T>(f_)) {}
template <class... A> decltype(auto) operator()(A&&... as) {
return f(ref(*this), forward<A>(as)...);
}
};
template <class F> decltype(auto) yc(F&& f) {
return ycr<decay_t<F>>(forward<F>(f));
} /// end-hash
// 0, ..., K-1
template <int K> struct Eertree {
struct Node {
array<int, K> ch;
int fail;
int l, r; // location of the first ocurrence
Node(int f_, int l_, int r_) : ch{}, fail(f_), l(l_), r(r_) {}
int len() const { return r-l; }
};
V<Node> x;
V<int> buf;
int cur;
Eertree(int alloc = 0) {
if (alloc) {
x.reserve(alloc+2);
buf.reserve(alloc);
}
x.emplace_back(-1, 1, 0);
x.emplace_back(0, 0, 0);
reset();
}
void reset() {
cur = 1;
buf.clear();
}
int append(int a) {
int i = int(buf.size());
buf.push_back(a);
auto works = [&](int v) -> bool {
int l = i - x[v].len();
return l > 0 && buf[l-1] == a;
};
for (; !works(cur); cur = x[cur].fail) {}
if (!x[cur].ch[a]) {
int par = x[cur].fail;
if (par != -1) {
for (; !works(par); par = x[par].fail) {}
}
int npar = (par == -1 ? 1 : x[par].ch[a]);
x[cur].ch[a] = int(x.size());
x.emplace_back(npar, i - x[cur].len() - 1, i + 1);
}
assert(x[cur].ch[a]);
cur = x[cur].ch[a];
return cur;
}
int size() const {
return int(x.size());
}
const Node& operator [](int i) const {
return x[i];
}
};
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int N;
cin >> N;
string S;
cin >> S;
S += S;
Eertree<10> et(2 * N);
V<int> locs; locs.reserve(2 * N);
for (char c : S) {
int v = int(c - '0');
assert(0 <= v && v < 10);
locs.push_back(et.append(v));
}
int nnodes = et.size();
V<int> f_before(nnodes);
V<int> f_all(nnodes);
for (int i = 0; i < N; i++) {
int v = locs[i];
f_before[v]++;
f_all[v]++;
}
for (int i = N; i < 2*N; i++) {
int v = locs[i];
f_all[v]++;
}
for (int v = nnodes-1; v >= 1; v--) {
int p = et[v].fail;
f_before[p] += f_before[v];
f_all[p] += f_all[v];
}
const ll MOD = 998244353;
ll ans = 0;
for (int v = 0; v < nnodes; v++) {
int len = et[v].len();
if (len <= N) {
ll f_cyc = f_all[v] - f_before[v];
f_cyc %= MOD;
ll val = len * ll(f_cyc) % MOD * f_cyc % MOD;
ans += val;
}
}
ans %= MOD;
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3556kb
input:
5 01010
output:
14
result:
wrong answer 1st numbers differ - expected: '39', found: '14'