forked from cp-algorithms/cp-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_manacher_odd.cpp
More file actions
74 lines (58 loc) · 1.88 KB
/
test_manacher_odd.cpp
File metadata and controls
74 lines (58 loc) · 1.88 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
#include "manacher_odd.h"
string getRandomString(size_t n, uint32_t seed, char minLetter='a', char maxLetter='b') {
assert(minLetter <= maxLetter);
const size_t nLetters = static_cast<int>(maxLetter) - static_cast<int>(minLetter) + 1;
std::uniform_int_distribution<size_t> distr(0, nLetters - 1);
std::mt19937 gen(seed);
string res;
res.reserve(n);
for (size_t i = 0; i < n; ++i)
res.push_back('a' + distr(gen));
return res;
}
bool testManacherOdd(const std::string &s) {
const auto n = s.size();
const auto d_odd = manacher_odd(s);
if (d_odd.size() != n)
return false;
const auto inRange = [&](size_t idx) {
return idx >= 0 && idx < n;
};
for (size_t i = 0; i < n; ++i) {
if (d_odd[i] < 0)
return false;
for (int d = 0; d < d_odd[i]; ++d) {
const auto idx1 = i - d;
const auto idx2 = i + d;
if (!inRange(idx1) || !inRange(idx2))
return false;
if (s[idx1] != s[idx2])
return false;
}
const auto idx1 = i - d_odd[i];
const auto idx2 = i + d_odd[i];
if (inRange(idx1) && inRange(idx2) && s[idx1] == s[idx2])
return false;
}
return true;
}
int main() {
vector<string> testCases;
testCases.push_back("");
for (size_t i = 1; i <= 25; ++i) {
auto s = string{};
s.resize(i, 'a');
testCases.push_back(move(s));
}
testCases.push_back("abba");
testCases.push_back("abccbaasd");
for (size_t n = 9; n <= 100; n += 10)
testCases.push_back(getRandomString(n, /* seed */ n, 'a', 'd'));
for (size_t n = 7; n <= 100; n += 10)
testCases.push_back(getRandomString(n, /* seed */ n));
for (const auto &s: testCases)
assert(testManacherOdd(s));
return 0;
}