summaryrefslogtreecommitdiff
path: root/test/string/lyndon.cpp
blob: 671097356ab541e90d059d132dd5deddf6d2db90 (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
#include "../util.h"
#include <string/lyndon.cpp>

bool isLyndon(string_view s) {
	string t = string(s) + string(s);
	for (ll i = 1; i < ssize(s); i++) {
		if (s >= t.substr(i, ssize(s))) return false;
	}
	return !s.empty();
}

vector<string> naive(ll n, char mi, char ma) {
	vector<string> res;
	auto dfs = [&](auto&& self, string pref)->void{
		if (ssize(pref) <= n && isLyndon(pref)) res.push_back(pref);
		if (ssize(pref) >= n) return;
		for (char c = mi; c <= ma; c++) {
			self(self, pref + c);
		}
	};
	dfs(dfs, "");
	return res;
}

vector<string> fast(ll n, char mi, char ma) {
	vector<string> res;
	string tmp(1, mi);
	do {
		res.push_back(tmp);
	} while (next(tmp, n, mi, ma));
	return res;
}

void stress_test() {
	ll queries = 0;
	for (ll i = 0; i < 10'000; i++) {
		int n = Random::integer<int>(1, 6);
		auto [l, r] = Random::pair<char>('a', 'f');
		auto got = fast(n, l, r);
		auto expected = naive(n, l, r);
		if (got != expected) cerr << "error" << FAIL;
		queries += ssize(expected);
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 9;
void performance_test() {
	timer t;
	t.start();
	auto res = fast(N, 'a', 'f');
	t.stop();
	hash_t hash = ssize(res);
	if (t.time > 500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	stress_test();
	performance_test();
}