summaryrefslogtreecommitdiff
path: root/test/string/suffixAutomaton.cpp
blob: dacbb83d398a2b838f9af49141f0a459fd739815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "../util.h"
#include <string/suffixAutomaton.cpp>

pair<int, int> naive(string_view s, string_view t) {
	int pos = 0;
	int len = 0;
	for (int j = 0; j < ssize(t); j++) {
		for (int i = 0; i < ssize(s); i++) {
			int cur = 0;
			while (i+cur < ssize(s) && j+cur < ssize(t) && s[i+cur] == t[j+cur]) cur++;
			if (cur > len) {
				pos = j;
				len = cur;
			}
		}
	}
	return {pos, len};
}

void stress_test() {
	ll queries = 0;
	for (int i = 0; i < 1000; i++) {
		int n = Random::integer<int>(1, 100);
		auto s = Random::string(n, "abc");
		SuffixAutomaton sa(s);
		for (int j = 0; j < 1000; j++) {
			int m = Random::integer<int>(1, 100);
			auto t = Random::string(m, "abc");
			auto got = sa.longestCommonSubstring(t);
			auto expected = naive(s, t);
			if (got != expected) cerr << "error" << FAIL;
			queries += m;
		}
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 500'000;
void performance_test() {
	timer t;
	auto s = Random::string(N, "a") + Random::string(N, "ab") + Random::string(N, "abcdefghijklmnopqrstuvwxyz");
	t.start();
	SuffixAutomaton sa(s);
	t.stop();
	hash_t hash = 0;
	for (ll c = 0; c < ssize(s);) {
		int m = Random::integer<int>(1, 1000);
		s = Random::string(m, "abc");
		t.start();
		auto [p, l] = sa.longestCommonSubstring(s);
		t.stop();
		hash += l + p;
		c += m;
	}
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	stress_test();
	if (!sanitize) performance_test();
}