#include<bits/stdc++.h>
#define ll long long
#define int long long
const int nmax = 1e6 + 5;
const ll oo = 1e18;
const int lg = 2;
const int base = 4000;
#define pii pair<int, int>
#define fi first
#define se second
#define debug(a, n) for(int i = 1; i <= n; ++i) cout << a[i] << ' ';
using namespace std;
const ll mod[] = {(ll)1e9 + 7, (ll)1e9 + 8277, (ll)1e9 + 8277, (ll)1e9+ 9277};
int n, cur = 0;
string s;
ll pw[nmax][lg], h[nmax][lg], rh[nmax][lg];
void Init(){
for(int j = 0; j < lg; ++j) pw[0][j] = 1;
for(int i = 1; i <= n; ++i){
for(int j= 0; j < lg; ++j){
pw[i][j] = pw[i - 1][j] * base % mod[j];
h[i][j] = (h[i - 1][j] * base + s[i] - 'a' + 1) % mod[j];
}
}
reverse(s.begin(), s.end());
for(int i = 1; i <= n; ++i){
for(int j = 0; j < lg; ++j){
rh[i][j] = (rh[i - 1][j] * base + s[i] - 'a' + 1) % mod[j];
}
}
}
ll get(int l, int r, ll h[][lg], int j){
return (h[r][j] - h[l - 1][j] * pw[r - l + 1][j] + mod[j] * mod[j]) % mod[j];
}
bool check(int l, int r){
for(int j = 0; j < lg; ++j){
if(get(l, r, h, j) != get(n - r + 1, n - l + 1, rh, j)) return 0;
}
return 1;
}
map<ll, ll> mp[lg], f, par, ind;
int cnt[nmax];
void update(int l, int r){
bool ok = 1;
while(l <= r){
int x = get(l, r, h, 0);
int y = get(l, r, h, 1);
if(++mp[0][x] == 1 && ++mp[1][y] == 1){
ind[x] = ++cur;
if(ok){
f[cur]++;
ok = 0;
}
cnt[cur] = r - l + 1;
if(l + 1 <= r - 1){
par[x] = get(l + 1, r - 1, h, 0);
}
}
else{
if(ok){
int id = ind[x];
f[id]++;
}
break;
}
++l, --r;
}
}
vector<int> adj[nmax];
void dfs(int u){
for(auto v : adj[u]){
dfs(v);
f[u] += f[v];
}
}
main(){
ios_base::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// freopen("code.inp", "r", stdin);
// freopen("code.out", "w", stdout);
cin >> s;
n = s.size(); s = ' ' + s + ' ';
Init();
for(int i = 1; i <= n; ++i){
int l = 1, r = min(i, n - i + 1), kq = 1;
while(l <= r){
int mid = r + l >> 1;
if(check(i - mid + 1, i + mid - 1)){
kq = mid;
l = mid + 1;
}
else r = mid - 1;
}
update(i - kq + 1, i + kq - 1);
l = 1, r = min(i, n - i), kq = -1;
while(l <= r){
int mid = r + l >> 1;
if(check(i - mid + 1, i + mid)){
kq = mid;
l = mid + 1;
}
else r = mid - 1;
}
if(kq != -1) update(i - kq + 1, i + kq);
}
for(auto [x, y] : mp[0]){
int v = ind[x];
int u = ind[par[x]];
adj[u].push_back(v);
}
dfs(0);
ll ans = 0;
for(int i = 1; i <= cur; ++i){
ans = max(ans, 1ll * f[i] * cnt[i]);
}
cout << ans;
}
/*
*/
Compilation message
palindrome.cpp:85:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
85 | main(){
| ^~~~
palindrome.cpp: In function 'int main()':
palindrome.cpp:96:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
96 | int mid = r + l >> 1;
| ~~^~~
palindrome.cpp:106:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
106 | int mid = r + l >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
29272 KB |
Output is correct |
2 |
Correct |
6 ms |
29276 KB |
Output is correct |
3 |
Correct |
6 ms |
29276 KB |
Output is correct |
4 |
Correct |
8 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29276 KB |
Output is correct |
6 |
Correct |
6 ms |
29272 KB |
Output is correct |
7 |
Correct |
6 ms |
29276 KB |
Output is correct |
8 |
Correct |
6 ms |
29276 KB |
Output is correct |
9 |
Correct |
6 ms |
29368 KB |
Output is correct |
10 |
Correct |
6 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29292 KB |
Output is correct |
12 |
Correct |
6 ms |
29312 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29288 KB |
Output is correct |
15 |
Correct |
6 ms |
29272 KB |
Output is correct |
16 |
Correct |
6 ms |
29276 KB |
Output is correct |
17 |
Correct |
7 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29288 KB |
Output is correct |
19 |
Correct |
7 ms |
29276 KB |
Output is correct |
20 |
Correct |
7 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29276 KB |
Output is correct |
22 |
Correct |
6 ms |
29276 KB |
Output is correct |
23 |
Correct |
7 ms |
29276 KB |
Output is correct |
24 |
Correct |
6 ms |
29276 KB |
Output is correct |
25 |
Correct |
6 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29276 KB |
Output is correct |
27 |
Correct |
7 ms |
29276 KB |
Output is correct |
28 |
Correct |
6 ms |
29272 KB |
Output is correct |
29 |
Correct |
6 ms |
29276 KB |
Output is correct |
30 |
Correct |
6 ms |
29276 KB |
Output is correct |
31 |
Correct |
7 ms |
29300 KB |
Output is correct |
32 |
Correct |
7 ms |
29368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
29532 KB |
Output is correct |
2 |
Correct |
8 ms |
29532 KB |
Output is correct |
3 |
Correct |
9 ms |
29532 KB |
Output is correct |
4 |
Correct |
8 ms |
29532 KB |
Output is correct |
5 |
Correct |
9 ms |
29560 KB |
Output is correct |
6 |
Correct |
8 ms |
29560 KB |
Output is correct |
7 |
Correct |
8 ms |
29532 KB |
Output is correct |
8 |
Correct |
8 ms |
29612 KB |
Output is correct |
9 |
Correct |
9 ms |
29528 KB |
Output is correct |
10 |
Correct |
7 ms |
29276 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
8 ms |
29704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
33424 KB |
Output is correct |
2 |
Correct |
30 ms |
33316 KB |
Output is correct |
3 |
Correct |
37 ms |
33216 KB |
Output is correct |
4 |
Correct |
36 ms |
33372 KB |
Output is correct |
5 |
Correct |
27 ms |
33312 KB |
Output is correct |
6 |
Correct |
27 ms |
33260 KB |
Output is correct |
7 |
Correct |
30 ms |
33112 KB |
Output is correct |
8 |
Correct |
11 ms |
29532 KB |
Output is correct |
9 |
Correct |
11 ms |
29608 KB |
Output is correct |
10 |
Correct |
21 ms |
32092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1070 ms |
71876 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1041 ms |
131072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |