#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int,int> pi;
char str[600005];
int n;
int dp[600005];
void manacher(){
int max_val = 0, pos = 0;
for (int i=1; i<=n; i++) {
int ret = i;
if(max_val >= i){
ret = min(max_val,i + dp[2 * pos - i]);
}
while (ret+1<=n && 2*i-ret-1> 0 && str[ret+1] == str[2*i-ret-1]){
ret++;
}
dp[i] = ret - i;
if(ret > max_val){
pos = i, ret = max_val;
}
}
for (int i=1; i<=n; i++) {
if(i%2 == 0) dp[i]++;
dp[i] /= 2;
}
}
const int prime = 409;
const long long modulo = 1e11 + 9;
long long hs[300005], pw[300005];
unordered_map<long long,int> len, cnt, num;
unordered_map<long long,long long> nxt;
vector<long long> hash_candidate;
int piv;
long long ara_lusse(long long b1, long long b2){
long long r = 0;
while (b1) {
if(b1&1) r += b2;
b1 >>= 1;
b2 <<= 1;
b2 %= modulo;
r %= modulo;
}
return r;
}
long long get_hash(int s, int e){
return ((hs[e] - ara_lusse(hs[s-1],pw[e - s + 1])) % modulo + modulo) % modulo;
}
void shrink(int s, int e, long long pa){
if(s > e) return;
long long par = 0;
num[0] = 0;
cnt[get_hash(s,e)]++;
while (s <= e) {
long long hsh = get_hash(s,e);
hash_candidate.push_back(hsh);
if(par) nxt[par] = hsh;
if(len.find(hsh) == len.end()){
len[hsh] = e - s + 1;
num[hsh] = ++piv;
}
else break;
par = hsh;
s++;
e--;
}
}
int tpar[300005], tcnt[300005], tlen[300005];
vector<int> graph[300005];
int subsize[300005];
long long ret;
int dfs(int x, int pa){
int r = tcnt[x];
for (int i=0; i<graph[x].size(); i++) {
if(graph[x][i] == pa) continue;
r += dfs(graph[x][i],x);
}
subsize[x] = r;
// printf("%d %d\n",subsize[x],tlen[x]);
ret = max(ret,1ll * subsize[x] * tlen[x]);
return r;
}
void make_tree(){
for (long long i : hash_candidate){
tpar[num[i]] = num[nxt[i]];
tcnt[num[i]] = cnt[i];
tlen[num[i]] = len[i];
}
for (int i=0; i<=piv; i++) {
if (i == tpar[i]) continue;
graph[i].push_back(tpar[i]);
graph[tpar[i]].push_back(i);
}
dfs(0,-1);
}
int main(){
memset(str,0,sizeof(str));
scanf("%s",str);
n = (int)strlen(str);
for (int i=n-1; i>=0; i--) {
str[2*i+1] = str[i];
str[2*i] = ' ';
}
n = 2 * n - 1;
str[0] = 0;
manacher();
pw[0] = 1;
for (int i=1; i<=(n+1)/2; i++) {
hs[i] = (hs[i-1] * prime + str[2*i-1] - 'a') % modulo;
pw[i] = pw[i-1] * prime % modulo;
}
for (int i=1; i<=n; i++) {
if(i % 2 == 1){
shrink((i+1)/2 - dp[i],(i+1)/2 + dp[i],0);
}
else{
shrink(i/2 + 1 - dp[i], i/2 + dp[i],0);
}
}
make_tree();
printf("%lld",ret);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20924 KB |
Output is correct - answer is '7' |
2 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '4' |
3 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '9' |
4 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '1' |
5 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '1' |
6 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '2' |
7 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '3' |
8 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '3' |
9 |
Correct |
0 ms |
20924 KB |
Output is correct - answer is '15' |
10 |
Correct |
4 ms |
20924 KB |
Output is correct - answer is '24' |
11 |
Incorrect |
2 ms |
20924 KB |
Output isn't correct - expected '10', found '28' |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |