이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define INF 1100000000
#define INFLL 1000000000000000000ll
#define ii pair<int,int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define M1 1000000007ll
#define M2 1000000009ll
#define UQ(x) (x).resize(distance((x).begin(), unique(all(x))))
#define rep(i,a,b) for (int i = (a); i < (b); i++)
template<typename T> bool ckmax(T& a, T const& b) {return b>a?a=b,1:0;}
template<typename T> bool ckmin(T& a, T const& b) {return b<a?a=b,1:0;}
using vi = vector<int>;
const int maxn = 300005, sigma = 26;
struct paltree {
string s = "$";
int cur = 0;
struct node {
int len, link, occ=0;
int to[26]={0};
node(int _len, int _link):len(_len),link(_link){}
};
vector<node> st = {{0,1},{-1,0}};
int last = 1;
vector<vi> to;
int get_link(int v) {
while(s[sz(s) - st[v].len - 2] != s.back()) v = st[v].link;
return v;
}
ll ans=0;
void add_char(char C) {
s += C;
int c = C-'a';
last = get_link(last);
if (!st[last].to[c]) {
st.emplace_back(st[last].len+2, st[get_link(st[last].link)].to[c]);
st[last].to[c] = sz(st)-1;
}
last = st[last].to[c];
int p = last;
while (p>1) {
st[p].occ++;
p = st[p].link;
}
// printf("update %d\n", last);
}
void dfs(int v) {
for (int i=0;i<26;i++) {
if (st[v].to[i]) {
dfs(st[v].to[i]);
st[v].occ += st[st[v].to[i]].occ;
}
}
printf("v = %d:\n", v);
for (int i=0;i<26;i++) {
if (st[v].to[i]) {
printf("has child %c: %d\n", i+'a', st[v].to[i]);
}
}
printf("occ = %d, len = %d\n", st[v].occ, st[v].len);
ans = max(ans, (ll)st[v].len * (ll)st[v].occ);
}
ll solve() {
for (auto &x:st) {
ckmax(ans, (ll)x.len*(ll)x.occ);
}
return ans;
}
};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
string s;
cin>>s;
paltree p;
for (int i=0;i<sz(s);i++) {
p.add_char(s[i]);
}
cout << p.solve() << '\n';
}
// int dp[maxn][2]; // dp(i,b) = min number of palindromes split with parity b
// int main() {
// scanf("%s",t);
// int l = strlen(t);
// init();
// for (int i = 0; i < l; i++) {
// add_letter(t[i]-'a');
// if (i == 0) {
// dp[0][0] = INF;
// dp[0][1] = 1;
// } else {
// }
// }
// printf("%s\n", t);
// }
# | 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... |