-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
check-if-dfs-strings-are-palindromes.cpp
135 lines (126 loc) · 4.17 KB
/
check-if-dfs-strings-are-palindromes.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Time: O(n)
// Space: O(n)
// iterative dfs, manacher's algorithm
class Solution {
public:
vector<bool> findAnswer(vector<int>& parent, string s) {
const auto& manacher = [](const string& s) {
const auto& preProcess = [](const string& s) {
if (empty(s)) {
return string("^$");
}
string ret = "^";
for (int i = 0; i < size(s); ++i) {
ret += "#" + s.substr(i, 1);
}
ret += "#$";
return ret;
};
string T = preProcess(s);
const int n = size(T);
vector<int> P(n);
int C = 0, R = 0;
for (int i = 1; i < n - 1; ++i) {
int i_mirror = 2 * C - i;
P[i] = (R > i) ? min(R - i, P[i_mirror]) : 0;
while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) {
++P[i];
}
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
return P;
};
vector<vector<int>> adj(size(parent));
for (int v = 1; v < size(parent); ++v) {
adj[parent[v]].emplace_back(v);
}
const auto& iter_dfs = [&]() {
int cnt = 0;
string curr;
vector<pair<int, int>> lookup(size(adj));
vector<tuple<int, int, int>> stk = {{1, 0, -1}};
while (!empty(stk)) {
const auto [step, u, left] = stk.back(); stk.pop_back();
if (step == 1) {
stk.emplace_back(2, u, cnt);
for (int i = size(adj[u]) - 1; i >= 0; --i) {
stk.emplace_back(1, adj[u][i], -1);
}
} else if (step == 2) {
curr.push_back(s[u]);
lookup[u] = pair(left, cnt++);
}
}
return pair(curr, lookup);
};
const auto& [curr, lookup] = iter_dfs();
const auto& P = manacher(curr);
vector<bool> result;
for (const auto& [left, right] : lookup) {
result.emplace_back(P[(2 * (left + 1) + 2 * (right + 1)) / 2] >= right - left + 1);
}
return result;
}
};
// Time: O(n)
// Space: O(n)
// dfs, manacher's algorithm
class Solution2 {
public:
vector<bool> findAnswer(vector<int>& parent, string s) {
const auto& manacher = [](const string& s) {
const auto& preProcess = [](const string& s) {
if (empty(s)) {
return string("^$");
}
string ret = "^";
for (int i = 0; i < size(s); ++i) {
ret += "#" + s.substr(i, 1);
}
ret += "#$";
return ret;
};
string T = preProcess(s);
const int n = size(T);
vector<int> P(n);
int C = 0, R = 0;
for (int i = 1; i < n - 1; ++i) {
int i_mirror = 2 * C - i;
P[i] = (R > i) ? min(R - i, P[i_mirror]) : 0;
while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) {
++P[i];
}
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
return P;
};
vector<vector<int>> adj(size(parent));
for (int v = 1; v < size(parent); ++v) {
adj[parent[v]].emplace_back(v);
}
int cnt = 0;
string curr;
vector<pair<int, int>> lookup(size(adj));
const function<void (int)> dfs = [&](int u) {
const int left = cnt;
for (const auto& v : adj[u]) {
dfs(v);
}
curr.push_back(s[u]);
lookup[u] = pair(left, cnt++);
};
dfs(0);
const auto& P = manacher(curr);
vector<bool> result;
for (const auto& [left, right] : lookup) {
result.emplace_back(P[(2 * (left + 1) + 2 * (right + 1)) / 2] >= right - left + 1);
}
return result;
}
};