-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathManacher.h
More file actions
34 lines (30 loc) · 730 Bytes
/
Copy pathManacher.h
File metadata and controls
34 lines (30 loc) · 730 Bytes
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
class Manacher
{
public:
Manacher(const string &s) : d(expand(s)), r(d.size())
{
int p = 0, mx = 0;
for (int i = 1; i < int(d.length()) - 1; i++) {
r[i] = mx > i ? min(r[2 * p - i], mx - i) : 1;
while (d[i + r[i]] == d[i - r[i]]) { r[i]++; }
if (i + r[i] > mx) { p = i; mx = i + r[i]; }
}
}
vector<int> &result()
{ return r; }
int &operator [](unsigned n)
{ return r[n]; }
private:
static string expand(const string &str)
{
string s("$#");
for (char c : str) {
s.push_back(c); s.push_back('#');
}
s.push_back('@');
return s;
}
protected:
string d;
vector<int> r;
};