이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 300005;
const int inf = 1e9 + 5;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
int n;
string s;
struct palindromic_tree {
int N;
int idx;
int last;
int a[maxn];
int len[maxn];
int cnt[maxn];
int fail[maxn];
int lenj[maxn];
int son[maxn][26];
int new_node(int X) {
memset(son[idx], 0, sizeof(son[idx]));
len[idx] = X;
cnt[idx] = 0;
return idx++;
}
void init() {
N = 0, last = 0, idx = 0;
new_node(0), new_node(-1);
fail[0] = 1;
a[0] = -1;
}
int get_fail(int X) {
while(a[N - len[X] - 1] != a[N])X = fail[X];
return X;
}
void add(int X) {
X -= 'a';
a[++N] = X;
int cur = get_fail(last);
if(!son[cur][X]) {
int nw = new_node(len[cur] + 2);
fail[nw] = son[get_fail(fail[cur])][X];
son[cur][X] = nw;
}
last = son[cur][X];
lenj[last] += 1;
}
void update() {
fb(i,idx - 1, 0) {
int X = i;
while(X != 0) {
cnt[X] += lenj[i];
X = fail[X];
}
lenj[i] = 0;
}
}
} P;
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> s;
n = sz(s);
P.init();
ff(i,0,n - 1)P.add(s[i]);
P.update();
ll rez = 0;
ff(i,0,P.idx - 1)rez = max(rez, 1ll * P.len[i] * P.cnt[i]);
cout << rez << endl;
return 0;
}
/**
abacaba
www
// probati bojenje sahovski ili slicno
**/
# | 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... |