Skip to content

Commit 1df43b8

Browse files
author
Yurii A.
committed
Add test for manacher_odd
1 parent b55c9c0 commit 1df43b8

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/string/manacher.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ For calculating $d_{odd}[]$, we get the following code. Things to note:
140140
- The while loop denotes the trivial algorithm. We launch it irrespective of the value of $k$.
141141
- If the size of palindrome centered at $i$ is $x$, then $d_{odd}[i]$ stores $\frac{x+1}{2}$.
142142
143-
```cpp
143+
```{.cpp file=manacher_odd}
144144
vector<int> manacher_odd(string s) {
145145
int n = s.size();
146146
s = "$" + s + "^";

test/test_manacher_odd.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
static 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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy