QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#291415#7876. Cyclic Substringsmendicillin2AC ✓159ms395488kbC++172.5kb2023-12-26 15:32:002023-12-26 15:32:00

Judging History

This is the latest submission verdict.

  • [2023-12-26 15:32:00]
  • Judged
  • Verdict: AC
  • Time: 159ms
  • Memory: 395488kb
  • [2023-12-26 15:32:00]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

template <class T> int sz(T&& a) { return int(size(forward<T>(a))); }

template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;

using ll = int64_t;

template <class F> struct ycr {
	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));
}

template <int K, class S> struct eertree {
	struct N {
		int suf, anc, l, r;
		array<int, K> ch;
		int len() const { return r-l; }
	};
	V<N> nodes;
	int cur;
	S buf;
	eertree(int alloc = 0) {
		if (alloc > 2) {
			nodes.reserve(alloc);
		}
		nodes.push_back({-1, -1, 1, 0, {}});
		nodes.push_back({0, -1, 0, 0, {}});
		reset();
	}
	void reset() {
		buf.clear();
		cur = 1;
	}

	int dist(int v) {
		return nodes[v].suf <= 0 ? -1 : nodes[v].len() - nodes[nodes[v].suf].len();
	}
	template <class A> int extend(A a) {
		int i = int(buf.size());
		buf.push_back(a);
		auto works = [&](int v) -> bool {
			int l = i - nodes[v].len();
			return l > 0 && buf[l-1] == buf[i];
		};
		while (!works(cur)) cur = nodes[cur].suf;
		if (!nodes[cur].ch[buf[i]]) {
			int d = nodes[cur].suf;
			if (d != -1) {
				while (!works(d)) d = nodes[d].suf;
			}
			int e = (d == -1 ? 1 : nodes[d].ch[buf[i]]);
			int f = nodes[cur].ch[buf[i]] = int(nodes.size());
			nodes.push_back({e, e, i - nodes[cur].len() - 1, i + 1, {}});
			if (dist(e) == dist(f)) nodes[f].anc = nodes[e].anc;
		}
		return cur = nodes[cur].ch[buf[i]];
	}

	const N& operator [] (int i) const {
		return nodes[i];
	}
};

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);

	int N;
	cin >> N;
	string S;
	cin >> S;

	eertree<10, V<int>> et(2*N+10);
	for (int i = 0; i < N; i++) {
		int a = int(S[i] - '0');
		assert(0 <= a && a < 10);
		et.extend(a);
	}
	V<int> locs(N);
	for (int i = 0; i < N; i++) {
		int a = int(S[i] - '0');
		assert(0 <= a && a < 10);
		locs[i] = et.extend(a);
	}

	int num_et = int(et.nodes.size());
	V<int> dp(num_et);
	for (int v : locs) {
		dp[v]++;
	}

	constexpr int MOD = 998244353;
	auto add = [&](int& a, int b) -> void {
		a += b;
		if (a >= MOD) a -= MOD;
	};

	int ans = 0;
	for (int v = num_et-1; v >= 2; v--) {
		add(dp[et[v].suf], dp[v]);
		int len = et[v].len();
		if (len <= N) {
			int cur = int(ll(dp[v]) * dp[v] % MOD * len % MOD);
			add(ans, cur);
		}
	}
	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: 3440kb

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3476kb

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3556kb

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3444kb

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: 3436kb

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 47ms
memory: 133412kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 119ms
memory: 394888kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 136ms
memory: 201036kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 112ms
memory: 395488kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 108ms
memory: 394848kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 159ms
memory: 351552kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 143ms
memory: 363928kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 85ms
memory: 216980kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 59ms
memory: 102412kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 100ms
memory: 339908kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 110ms
memory: 332408kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed