#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
using ll = long long;
// cnt - number of palindromic suffixes of the node.
// make string 1 indexed.
struct PalindromicTree {
struct node {
int nxt[26], len, st, en, link, cnt, oc;
};
string s;
vector<node>t;
int sz, last;
PalindromicTree() {}
PalindromicTree( string _s ) {
s = _s;
int n = s.size();
t.resize( n + 5, node() );
sz = 2;
last = 2;
t[1].len = -1, t[1].link = 1;
t[2].len = 0, t[2].link = 1;
}
// returns 1 if new palindrome is found
int extend( int pos ) {
while (s[pos - t[last].len - 1] != s[pos]) last = t[last].link;
int ch = s[pos] - 'a', x = t[last].link;
while (s[pos - t[x].len - 1] != s[pos]) x = t[x].link;
if (t[last].nxt[ch]) {
last = t[last].nxt[ch];
t[last].oc++;
return 0;
}
sz++;
t[sz].oc = 1;
t[sz].len = t[last].len + 2;
t[last].nxt[ch] = sz;
last = sz;
t[sz].en = pos;
t[sz].st = pos - t[sz].len + 1;
if (t[sz].len == 1) {
t[sz].link = 2, t[sz].cnt = 1;
} else {
t[sz].cnt = 1 + t[x].cnt, t[sz].link = t[x].nxt[ch];
}
return 1;
}
void calc_occ() {
for (int i = sz; i >= 3; i--) t[t[i].link].oc += t[i].oc;
}
ll ans(){
ll ret = 0;
for(int i=3;i<=sz;i++){
ret = max(ret,1ll*t[i].oc*t[i].len*1ll);
}
return ret;
}
};
int main() {
string str;
cin >> str;
str = '#' + str;
PalindromicTree pt( str );
for (int i = 1; i < str.size(); i++) {
pt.extend( i );
}
pt.calc_occ();
cout<<pt.ans()<<endl;
return 0;
}
Compilation message
palindrome.cpp: In function 'int main()':
palindrome.cpp:65:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for (int i = 1; i < str.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
284 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1484 KB |
Output is correct |
3 |
Correct |
2 ms |
1484 KB |
Output is correct |
4 |
Correct |
1 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
1484 KB |
Output is correct |
6 |
Correct |
1 ms |
1484 KB |
Output is correct |
7 |
Correct |
1 ms |
1484 KB |
Output is correct |
8 |
Correct |
1 ms |
1484 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
1 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
13128 KB |
Output is correct |
2 |
Correct |
11 ms |
13000 KB |
Output is correct |
3 |
Correct |
11 ms |
13000 KB |
Output is correct |
4 |
Correct |
11 ms |
13212 KB |
Output is correct |
5 |
Correct |
12 ms |
13228 KB |
Output is correct |
6 |
Correct |
11 ms |
13108 KB |
Output is correct |
7 |
Correct |
12 ms |
13232 KB |
Output is correct |
8 |
Correct |
12 ms |
13128 KB |
Output is correct |
9 |
Correct |
12 ms |
13212 KB |
Output is correct |
10 |
Correct |
11 ms |
13184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
38720 KB |
Output is correct |
2 |
Correct |
30 ms |
38932 KB |
Output is correct |
3 |
Correct |
30 ms |
38932 KB |
Output is correct |
4 |
Correct |
36 ms |
38928 KB |
Output is correct |
5 |
Correct |
35 ms |
39024 KB |
Output is correct |
6 |
Correct |
32 ms |
38976 KB |
Output is correct |
7 |
Correct |
32 ms |
38944 KB |
Output is correct |
8 |
Correct |
26 ms |
38976 KB |
Output is correct |
9 |
Correct |
25 ms |
38968 KB |
Output is correct |
10 |
Correct |
31 ms |
38944 KB |
Output is correct |
11 |
Correct |
30 ms |
38976 KB |
Output is correct |
12 |
Correct |
31 ms |
38928 KB |
Output is correct |