#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
const int N = 300'010;
const int alpha = 26;
int longest_palin[N];
int second_longest[N];
string s;
int n;
struct node {
int link = 0;
int next[alpha] = {};
int len = 0;
int cnt = 0;
bool vis = 0;
} pool[10000000];
int rt = 1;
int make_node()
{
static int nxt = 2;
return nxt++;
}
int lst_node = rt;
vector<int> all_nodes = {rt};
void insert_node(char c)
{
c -= 'a';
int new_node = make_node();
all_nodes.push_back(new_node);
pool[new_node].cnt = 1;
pool[new_node].len = pool[lst_node].len + 1;
int cur = lst_node;
lst_node = new_node;
while (cur) {
if (!pool[cur].next[c]) {
pool[cur].next[c] = new_node;
cur = pool[cur].link;
} else {
break;
}
}
if (!cur) {
pool[new_node].link = rt;
return;
}
int q = pool[cur].next[c];
if (pool[cur].len + 1 == pool[q].len) {
pool[new_node].link = q;
return;
}
int clone = make_node();
all_nodes.push_back(clone);
pool[clone] = pool[q];
pool[clone].cnt = 0;
pool[clone].len = pool[cur].len + 1;
pool[q].link = clone;
while (cur && pool[cur].next[c] == q) {
pool[cur].next[c] = clone;
cur = pool[cur].link;
}
pool[new_node].link = clone;
}
int index2node[N];
void make_automata()
{
Loop (i,0,n) {
insert_node(s[i]);
index2node[i] = lst_node;
}
sort(all_nodes.begin(), all_nodes.end(), [](int a, int b) {
return pool[a].len < pool[b].len;
});
LoopR (i,1,all_nodes.size()) {
int t = all_nodes[i];
pool[pool[t].link].cnt += pool[t].cnt;
}
}
void find_palin(int i)
{
int j = i-1;
while (longest_palin[j] == i || s[i-longest_palin[j]-1] != s[i]) {
if (second_longest[j] == -1) {
if (s[i] == s[i-1]) {
longest_palin[i] = 2;
second_longest[i] = j;
} else {
longest_palin[i] = 1;
second_longest[i] = -1;
}
return;
}
j = second_longest[j];
}
longest_palin[i] = longest_palin[j]+2;
int sec_len = -1;
do {
if (second_longest[j] == -1) {
sec_len = 1 + (s[i] == s[j]);
break;
}
j = second_longest[j];
} while (longest_palin[j] == i || s[i-longest_palin[j]-1] != s[i]);
if (sec_len == -1)
sec_len = longest_palin[j] + 2;
j = i - longest_palin[i] + sec_len;
while (longest_palin[j] != sec_len)
j = second_longest[j];
second_longest[i] = j;
}
void find_all_palins()
{
longest_palin[0] = 1;
second_longest[0] = -1;
Loop (i,1,n)
find_palin(i);
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> s;
n = s.size();
make_automata();
find_all_palins();
ll ans = 0;
Loop (i,0,n) {
int t = index2node[i];
while (pool[pool[t].link].len >= longest_palin[i]) {
pool[t].vis = 1;
t = pool[t].link;
if (pool[t].vis)
break;
}
if (pool[t].vis)
continue;
ans = max(ans, (ll)longest_palin[i] * pool[t].cnt);
}
cout << ans << '\n';
}
Compilation message
palindrome.cpp: In function 'void insert_node(char)':
palindrome.cpp:44:23: warning: array subscript has type 'char' [-Wchar-subscripts]
44 | if (!pool[cur].next[c]) {
| ^
palindrome.cpp:45:19: warning: array subscript has type 'char' [-Wchar-subscripts]
45 | pool[cur].next[c] = new_node;
| ^
palindrome.cpp:55:25: warning: array subscript has type 'char' [-Wchar-subscripts]
55 | int q = pool[cur].next[c];
| ^
palindrome.cpp:66:31: warning: array subscript has type 'char' [-Wchar-subscripts]
66 | while (cur && pool[cur].next[c] == q) {
| ^
palindrome.cpp:67:18: warning: array subscript has type 'char' [-Wchar-subscripts]
67 | pool[cur].next[c] = clone;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
480 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
476 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
472 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1748 KB |
Output is correct |
2 |
Correct |
3 ms |
2692 KB |
Output is correct |
3 |
Correct |
2 ms |
1756 KB |
Output is correct |
4 |
Correct |
3 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
1760 KB |
Output is correct |
6 |
Correct |
3 ms |
2580 KB |
Output is correct |
7 |
Correct |
3 ms |
3028 KB |
Output is correct |
8 |
Correct |
2 ms |
2004 KB |
Output is correct |
9 |
Correct |
2 ms |
2020 KB |
Output is correct |
10 |
Correct |
3 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
13996 KB |
Output is correct |
2 |
Correct |
31 ms |
19272 KB |
Output is correct |
3 |
Correct |
14 ms |
14088 KB |
Output is correct |
4 |
Correct |
31 ms |
18368 KB |
Output is correct |
5 |
Correct |
15 ms |
14096 KB |
Output is correct |
6 |
Correct |
27 ms |
18692 KB |
Output is correct |
7 |
Correct |
36 ms |
23424 KB |
Output is correct |
8 |
Correct |
36 ms |
17416 KB |
Output is correct |
9 |
Correct |
32 ms |
19464 KB |
Output is correct |
10 |
Correct |
33 ms |
20228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
40988 KB |
Output is correct |
2 |
Correct |
111 ms |
72692 KB |
Output is correct |
3 |
Correct |
45 ms |
41064 KB |
Output is correct |
4 |
Correct |
115 ms |
65096 KB |
Output is correct |
5 |
Correct |
55 ms |
41060 KB |
Output is correct |
6 |
Correct |
102 ms |
67528 KB |
Output is correct |
7 |
Correct |
120 ms |
73916 KB |
Output is correct |
8 |
Correct |
120 ms |
51332 KB |
Output is correct |
9 |
Correct |
113 ms |
51320 KB |
Output is correct |
10 |
Correct |
95 ms |
57032 KB |
Output is correct |
11 |
Correct |
45 ms |
41008 KB |
Output is correct |
12 |
Correct |
113 ms |
53404 KB |
Output is correct |