#include <bits/stdc++.h>
using namespace std;
struct eerTree {
struct state {
int nxt[26], len, link, cnt;
state(int len, int link, int cnt):
len(len), link(link), cnt(cnt) {
memset(nxt, 0, sizeof nxt);
}
};
vector <state> tree;
string s; int last;
vector <int> topo;
void extend(int pos) {
int chr = s[pos] - 'a', cur = last;
while (pos - tree[cur].len <= 0 ||
s[pos - tree[cur].len - 1] != s[pos])
cur = tree[cur].link;
if (tree[cur].nxt[chr]) {
last = tree[cur].nxt[chr];
tree[last].cnt++; return;
}
last = tree.size();
tree.emplace_back(tree[cur].len + 2, 0, 1);
tree[cur].nxt[chr] = last;
if (cur == 0) {
tree[last].link = 1; return;
}
cur = tree[cur].link;
while (pos - tree[cur].len <= 0 ||
s[pos - tree[cur].len - 1] != s[pos])
cur = tree[cur].link;
tree[last].link = tree[cur].nxt[chr];
}
eerTree(string s): s(s), last(1) {
tree.emplace_back(-1, 0, 0);
tree.emplace_back(0, 0, 0);
for (int i = 0; i < s.size(); i++)
extend(i);
vector <int> cnt(s.size() + 1);
for (int i = 2; i < tree.size(); i++)
cnt[tree[i].len]++;
for (int i = 1; i <= s.size(); i++)
cnt[i] += cnt[i - 1];
topo.resize(tree.size() - 2);
iota(topo.begin(), topo.end(), 2);
for (int i = 2; i < tree.size(); i++)
topo[--cnt[tree[i].len]] = i;
for (int i = topo.size() - 1; i >= 0; i--) {
int cur = topo[i];
tree[tree[cur].link].cnt += tree[cur].cnt;
}
}
long long maxOccurrenceValue() {
long long val = 0;
for (int i : topo)
val = max(val, 1ll * tree[i].len * tree[i].cnt);
return val;
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
string s; cin >> s; eerTree eT(s);
cout << eT.maxOccurrenceValue() << '\n';
}
Compilation message
palindrome.cpp: In constructor 'eerTree::eerTree(std::string)':
palindrome.cpp:44:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i = 0; i < s.size(); i++)
| ~~^~~~~~~~~~
palindrome.cpp:47:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<eerTree::state>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 2; i < tree.size(); i++)
| ~~^~~~~~~~~~~~~
palindrome.cpp:49:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i = 1; i <= s.size(); i++)
| ~~^~~~~~~~~~~
palindrome.cpp:53:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<eerTree::state>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i = 2; i < tree.size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
392 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2380 KB |
Output is correct |
2 |
Correct |
3 ms |
2380 KB |
Output is correct |
3 |
Correct |
3 ms |
2400 KB |
Output is correct |
4 |
Correct |
3 ms |
2380 KB |
Output is correct |
5 |
Correct |
3 ms |
2380 KB |
Output is correct |
6 |
Correct |
4 ms |
2380 KB |
Output is correct |
7 |
Correct |
3 ms |
2380 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
2 ms |
492 KB |
Output is correct |
10 |
Correct |
3 ms |
1440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
15716 KB |
Output is correct |
2 |
Correct |
43 ms |
15716 KB |
Output is correct |
3 |
Correct |
26 ms |
15716 KB |
Output is correct |
4 |
Correct |
44 ms |
15716 KB |
Output is correct |
5 |
Correct |
26 ms |
15716 KB |
Output is correct |
6 |
Correct |
19 ms |
15716 KB |
Output is correct |
7 |
Correct |
22 ms |
15716 KB |
Output is correct |
8 |
Correct |
4 ms |
1388 KB |
Output is correct |
9 |
Correct |
8 ms |
4652 KB |
Output is correct |
10 |
Correct |
24 ms |
15716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
61196 KB |
Output is correct |
2 |
Correct |
103 ms |
61224 KB |
Output is correct |
3 |
Correct |
91 ms |
61224 KB |
Output is correct |
4 |
Correct |
95 ms |
61224 KB |
Output is correct |
5 |
Correct |
91 ms |
61252 KB |
Output is correct |
6 |
Correct |
85 ms |
61252 KB |
Output is correct |
7 |
Correct |
62 ms |
32068 KB |
Output is correct |
8 |
Correct |
11 ms |
3060 KB |
Output is correct |
9 |
Correct |
10 ms |
3060 KB |
Output is correct |
10 |
Correct |
63 ms |
31528 KB |
Output is correct |
11 |
Correct |
94 ms |
61224 KB |
Output is correct |
12 |
Correct |
15 ms |
6120 KB |
Output is correct |