-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manacher.cpp
68 lines (58 loc) · 1.3 KB
/
Manacher.cpp
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
#include <bits/stdc++.h>
using namespace std;
// Find all sub-palindrome, O(n)
// m_even[i] = (m[2i] - 1) / 2, m_odd[i] = m[2i + 1] / 2
vector<int> manacher(const string &s) {
string t = "#";
for (auto c : s) {
t += c;
t += '#';
}
int n = t.size();
vector<int> m(n);
for (int i = 0, j = 0; i < n; i++) {
if (2 * j - i >= 0 && j + m[j] > i) {
m[i] = min(m[2 * j - i], j + m[j] - i);
}
while (i - m[i] >= 0 && i + m[i] < n && t[i - m[i]] == t[i + m[i]]) {
m[i]++;
}
if (i + m[i] > j + m[j]) {
j = i;
}
}
return m;
}
void solve() {
string s;
cin >> s;
vector<int> m = manacher(s);
auto is_palindrome = [&](int l, int r) -> bool {
int len = r - l + 1, mid = l + r + 1;
int num = (m[mid] - (len % 2 == 0)) / 2;
return (2 * num - len % 2) >= len;
};
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
if (is_palindrome(l, r)) {
cout << "YES\n";
}
else {
cout << "NO\n";
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}