|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +#include "manacher_odd.h" |
| 5 | + |
| 6 | +string getRandomString(size_t n, uint32_t seed, char minLetter='a', char maxLetter='b') { |
| 7 | + assert(minLetter <= maxLetter); |
| 8 | + const size_t nLetters = static_cast<int>(maxLetter) - static_cast<int>(minLetter) + 1; |
| 9 | + std::uniform_int_distribution<size_t> distr(0, nLetters - 1); |
| 10 | + std::mt19937 gen(seed); |
| 11 | + |
| 12 | + string res; |
| 13 | + res.reserve(n); |
| 14 | + |
| 15 | + for (size_t i = 0; i < n; ++i) |
| 16 | + res.push_back('a' + distr(gen)); |
| 17 | + |
| 18 | + return res; |
| 19 | +} |
| 20 | + |
| 21 | +bool testManacherOdd(const std::string &s) { |
| 22 | + const auto n = s.size(); |
| 23 | + const auto d_odd = manacher_odd(s); |
| 24 | + |
| 25 | + if (d_odd.size() != n) |
| 26 | + return false; |
| 27 | + |
| 28 | + const auto inRange = [&](size_t idx) { |
| 29 | + return idx >= 0 && idx < n; |
| 30 | + }; |
| 31 | + |
| 32 | + for (size_t i = 0; i < n; ++i) { |
| 33 | + if (d_odd[i] < 0) |
| 34 | + return false; |
| 35 | + for (int d = 0; d < d_odd[i]; ++d) { |
| 36 | + const auto idx1 = i - d; |
| 37 | + const auto idx2 = i + d; |
| 38 | + |
| 39 | + if (!inRange(idx1) || !inRange(idx2)) |
| 40 | + return false; |
| 41 | + if (s[idx1] != s[idx2]) |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + const auto idx1 = i - d_odd[i]; |
| 46 | + const auto idx2 = i + d_odd[i]; |
| 47 | + if (inRange(idx1) && inRange(idx2) && s[idx1] == s[idx2]) |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + return true; |
| 52 | +} |
| 53 | + |
| 54 | +int main() { |
| 55 | + vector<string> testCases; |
| 56 | + |
| 57 | + testCases.push_back(""); |
| 58 | + for (size_t i = 1; i <= 25; ++i) { |
| 59 | + auto s = string{}; |
| 60 | + s.resize(i, 'a'); |
| 61 | + testCases.push_back(move(s)); |
| 62 | + } |
| 63 | + testCases.push_back("abba"); |
| 64 | + testCases.push_back("abccbaasd"); |
| 65 | + for (size_t n = 9; n <= 100; n += 10) |
| 66 | + testCases.push_back(getRandomString(n, /* seed */ n, 'a', 'd')); |
| 67 | + for (size_t n = 7; n <= 100; n += 10) |
| 68 | + testCases.push_back(getRandomString(n, /* seed */ n)); |
| 69 | + |
| 70 | + for (const auto &s: testCases) |
| 71 | + assert(testManacherOdd(s)); |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
0 commit comments