This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
$$$$$$$\ $$\ $$$$$$$\
$$ __$$\ \__| $$ __$$\
$$ | $$ | $$$$$$\ $$$$$$\ $$\ $$$$$$$\ $$ | $$ | $$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$\
$$$$$$$\ |$$ __$$\ $$ __$$\ $$ |$$ _____|$$$$$$$\ | \____$$\ $$ __$$\ $$ _____|\____$$\
$$ __$$\ $$ / $$ |$$ | \__|$$ |\$$$$$$\ $$ __$$\ $$$$$$$ |$$ | \__|$$ / $$$$$$$ |
$$ | $$ |$$ | $$ |$$ | $$ | \____$$\ $$ | $$ |$$ __$$ |$$ | $$ | $$ __$$ |
$$$$$$$ |\$$$$$$ |$$ | $$ |$$$$$$$ |$$$$$$$ |\$$$$$$$ |$$ | \$$$$$$$\\$$$$$$$ |
\_______/ \______/ \__| \__|\_______/ \_______/ \_______|\__| \_______|\_______|
*/
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define INS insert
#define LB lower_bound
#define UB upper_bound
#define pii pair <int,int>
#define pll pair <long long, long long>
#define X first
#define Y second
#define _ << " " <<
#define sz(x) (int)x.size()
#define all(a) (a).begin(),(a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i > (b); --i)
#define FORA(i, x) for (auto &i : x)
#define REP(i, n) FOR(i, 0, n)
#define BITS(x) __builtin_popcount(x)
#define SQ(a) (a) * (a)
#define TRACE(x) cout << #x " = " << (x) << '\n';
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define umap unordered_map
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <pii> vpi;
typedef vector <ll> vll;
typedef vector <pll> vpl;
typedef vector <string> vs;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
const int INF = 1e9 + 10;
const ll INFL = 1e18 + 10;
const int ABC = 30;
inline int sum(int a, int b){
if (a + b < 0)
return (a + b + MOD) % MOD;
return (a + b) % MOD;
}
inline void add(int &a, int b){
a = sum(a, b);
}
inline int mul(ll a, ll b){
return ((a % MOD) * ((ll)b % MOD)) % MOD;
}
inline int sub(int a, int b){
return (a - b + MOD) % MOD;
}
inline int fast_pot(ll pot, ll n){
ll ret = 1;
while (n){
if (n & 1LL)
ret = (ret * pot) % MOD;
pot = (pot * pot) % MOD;
n >>= 1LL;
}
return ret;
}
//radim palindromsko stablo
const int N = 5e5 + 10;
struct node{
int next[26];
int len;
int sufflink;
int num;
int lazy;
};
int len;
string s;
node tree[N];
int num;
int suff;
ll ans;
bool addLetter(int pos){
int cur = suff, curlen = 0;
int let = s[pos] - 'a';
while (true){
curlen = tree[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos])
break;
cur = tree[cur].sufflink;
}
if (tree[cur].next[let]){
suff = tree[cur].next[let];
tree[tree[cur].next[let]].lazy++;
return false;
}
num++;
suff = num;
tree[num].len = tree[cur].len + 2;
tree[cur].next[let] = num;
if (tree[num].len == 1){
tree[num].sufflink = 2;
tree[num].num = 1;
tree[num].lazy++;
return true;
}
while (true){
cur = tree[cur].sufflink;
curlen = tree[cur].len;
if (pos - 1 - curlen >= 0 && s[pos - 1 - curlen] == s[pos]){
tree[num].sufflink = tree[cur].next[let];
break;
}
}
tree[num].num = 1 + tree[tree[num].sufflink].num;
tree[num].lazy++;
}
void initTree(){
num = 2, suff = 2;
tree[1].len = -1, tree[1].sufflink = 1;
tree[2].len = 0, tree[2].sufflink = 1;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
len = sz(s);
initTree();
REP(i, len){
addLetter(i);
//ans += tree[suff].num;
}
ll sol = 0;
for (int i = num; i > 2; --i){
//cout << i _ tree[i].len _ tree[i].lazy << '\n';
sol = max(sol, (ll)tree[i].lazy * tree[i].len);
tree[tree[i].sufflink].lazy += tree[i].lazy;
}
cout << sol << '\n';
return 0;
}
Compilation message (stderr)
palindrome.cpp: In function 'bool addLetter(int)':
palindrome.cpp:138:16: warning: control reaches end of non-void function [-Wreturn-type]
138 | tree[num].lazy++;
| ~~~~~~~~~~~~~~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |