# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
941226 | wii | Palindromes (APIO14_palindrome) | C++17 | 36 ms | 69092 KiB |
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;
typedef double db;
typedef long long ll;
typedef long double ld;
#define int ll
typedef pair<int, int> pii;
#define lx (id << 1)
#define rx (lx | 1)
#define gcd __gcd
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define bit(i, mask) ((mask) >> (i) & 1)
#define reset(x, val) memset(x, val, sizeof(x))
#define foru(i,a,b) for(int i = (a); i <= (b); ++i)
#define ford(i,a,b) for(int i = (a); i >= (b); --i)
#define FastIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }
const ll Linf = 0x3f3f3f3f3f3f3f3f;
const int Inf = 0x3f3f3f3f;
const ll Mod = 1e9 + 7;
const ll Mod2 = ll(1e9) + 9;
const int Lim = 1e6 + 5;
const int inv6 = 166666668;
/// ====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====
const int base = 3;
const int N = 3e5 + 5;
const int K = log2(N) + 1;
const int dx[] = {+1, -1, 0, 0};
const int dy[] = {0, 0, +1, -1};
const int block_size = sqrt(2e9) + 2;
struct Node {
int len;
int suffLink;
int num;
int nxt[26];
};
int num;
int len;
int suff;
Node tree[N];
string str;
bool addLetter(int pos) {
int cur = suff, curLen = 0;
int let = str[pos] - 'a';
while (true) {
curLen = tree[cur].len;
if (pos - 1 - curLen >= 0 && str[pos - 1 - curLen] == str[pos])
break;
cur = tree[cur].suffLink;
}
if (tree[cur].nxt[let]) {
suff = tree[cur].nxt[let];
return false;
}
++num;
suff = num;
tree[num].len = tree[cur].len + 2;
tree[cur].nxt[let] = num;
if (tree[num].len == 1) {
tree[num].suffLink = 2;
return true;
}
while (true) {
cur = tree[cur].suffLink;
curLen = tree[cur].len;
if (pos - 1 - curLen >= 0 && str[pos - 1 - curLen] == str[pos]) {
tree[num].suffLink = tree[cur].nxt[let];
break;
}
}
return true;
}
void solve() {
cin >> str;
num = 2; suff = 2;
tree[1].len = -1; tree[2].suffLink = 1;
tree[2].len = 0; tree[2].suffLink = 1;
for (int i = 0; i < str.size(); ++i) {
addLetter(i);
++tree[suff].num;
}
int ans = 0;
for (int i = num; i >= 3; --i) {
tree[tree[i].suffLink].num += tree[i].num;
maximize(ans, tree[i].len * tree[i].num);
}
cout << ans;
}
signed main() {
FastIO;
#define task "test"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
#define task "palindrome"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
// cin >> ntest;
while (ntest--) {
//cout << "Case " << q << ": " << "\n";
solve();
cout << "\n";
}
return 0;
}
/** /\_/\
* (= ._.)
* / >TL \>AC
**/
Compilation message (stderr)
# | 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... |