이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("avx2")
#pragma GCC optimize("unroll-loops")
#define all(x) x.begin() , x.end()
#define sze(x) (ll)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
const ll maxn = 3e5 + 16 , P = 131 , md = 2000000357 , inf = 2e8;
struct mintree {
int sz = 1;
vector<pii> val;
pii def;
void init(int n){
while(sz < n) sz <<= 1;
def = {inf , -1};
val.assign(sz << 1 , def);
return;
}
void build(vector<int> &a , int x , int lx , int rx){
if(rx - lx == 1){
if(lx < sze(a)){
val[x] = {a[lx] , lx};
}
return;
}
int m = (rx + lx) >> 1 , ln = (x << 1) + 1 , rn = ln + 1;
build(a , ln , lx , m); build(a , rn , m , rx);
val[x] = min(val[ln] , val[rn]);
}
void build(vector<int> &a){
build(a , 0 , 0 , sz);
return;
}
pii cal(int l , int r , int x , int lx , int rx){
if(rx <= l || lx >= r) return def;
if(rx <= r && lx >= l) return val[x];
int m = (rx + lx) >> 1 , ln = (x << 1) + 1 , rn = ln + 1;
pii a = cal(l , r , ln , lx , m) , b = cal(l , r , rn , m , rx);
return min(a , b);
}
pii cal(int l , int r){
return cal(l , r , 0 , 0 , sz);
}
};
struct segtree {
int sz = 1;
vector<int> val;
void init(int n){
while(sz < n) sz <<= 1;
val.assign(sz << 1 , inf);
return;
}
void build(vector<int> &a , int x , int lx , int rx){
if(rx - lx == 1){
if(lx < sze(a)){
val[x] = a[lx];
}
return;
}
int m = (rx + lx) >> 1 , ln = (x << 1) + 1 , rn = ln + 1;
build(a , ln , lx , m); build(a , rn , m , rx);
val[x] = min(val[ln] , val[rn]);
}
void build(vector<int> &a){
build(a , 0 , 0 , sz);
return;
}
int cal(int l , int r , int k , int x , int lx , int rx){
if(rx <= l || lx >= r) return -1;
if(val[x] > k) return -1;
if(rx - lx == 1) return lx;
int m = (rx + lx) >> 1 , ln = (x << 1) + 1 , rn = ln + 1;
int a = cal(l , r , k , rn , m , rx);
if(a != -1) return a;
return cal(l , r , k , ln , lx , m);
}
int cal(int l , int r , int k){
return cal(l , r , k , 0 , 0 , sz);
}
};
int n;
string s;
mintree mt;
segtree st[2];
ll hs[maxn] , rh[maxn] , tv[maxn];
int sf[maxn];
vector<int> lcp;
int hum = -1;
int get_hash(int l , int r){
if(r >= n) return hum--;
ll res = hs[r] - (l ? tv[r - l + 1] * hs[l - 1] : 0);
res %= md; res += (res < 0) * md;
return res;
}
bool is_palindrome(int l , int r){
if(l < 0 || r >= n) return false;
ll h = get_hash(l , r);
ll o = rh[r] - (l ? rh[l - 1] : 0);
o %= md; o += (o < 0) * md;
h *= tv[l]; h %= md;
return (h == o);
}
int get_lcp(int i , int j){
int l = 0 , r = n;
while(l < r - 1){
int m = (r + l) >> 1;
if(get_hash(i , i + m - 1) == get_hash(j , j + m - 1)){
l = m;
} else {
r = m;
}
}
return l;
}
int t[maxn][2];
ll query(int i , int k){
if(k < 2) return k;
int res = 0;
int l = i , r = i + (k + 1) / 2;
int h = st[0].cal(l , r , i);
if(h != -1){
res = max(res , 2 * (h - i) + 1);
}
r = i + k / 2;
h = st[1].cal(l , r , i);
// cout<<l<<' '<<r<<' '<<i<<' '<<t[l][1]<<' '<<h<<'\n';
if(h != -1){
res = max(res , 2 * (h - i) + 2);
}
return res;
}
ll ans = 0;
void dv(int l , int r){
if(l == r - 1){
ans = max(ans , query(sf[l] , n - sf[l]));
}
if(l >= r - 1) return;
pii h = mt.cal(l , r - 1);
int k = h.first , m = h.second + 1;
ans = max(ans , 1ll * (r - l) * query(sf[l] , k));
dv(l , m); dv(m , r);
return;
}
int rnk[maxn] , rhk[maxn];
vector<int> cnt[maxn] , v;
int cal_t(int i , bool j){
int l = -1 , r = i + j;
while(l < r - 1){
int m = (r + l) >> 1;
if(is_palindrome(m , i + i - m + j)){
r = m;
} else {
l = m;
}
}
return r;
}
vector<int> a;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
tv[0] = 1;
for(int i = 1 ; i < maxn ; i++){
tv[i] = tv[i - 1] * P % md;
}
cin>>s; n = sze(s);
for(int i = 0 ; i < n ; i++){
rnk[i] = s[i] - 'a' + 1;
}
int lm = max(27 , n + 1);
for(int j = 0 ; j < 19 ; j++){
v.clear();
for(int i = 0 ; i < lm ; i++){
cnt[i].clear(); cnt[i].shrink_to_fit();
}
for(int i = 0 ; i < n ; i++){
int o = i + (1 << j);
rhk[i] = (o >= n ? 0 : rnk[o]);
cnt[rhk[i]].push_back(i);
}
for(int e = 0 ; e < lm ; e++){
for(auto i : cnt[e]){
v.push_back(i);
}
}
for(int i = 0 ; i < lm ; i++){
cnt[i].clear(); cnt[i].shrink_to_fit();
}
for(auto i : v){
cnt[rnk[i]].push_back(i);
}
int cur = 0;
for(int e = 0 ; e < lm ; e++){
int pr = -1;
for(auto i : cnt[e]){
int u = rhk[i];
if(pr != u){
cur++; pr = u;
}
rnk[i] = cur;
}
}
}
for(int i = 0 ; i < n ; i++){
sf[rnk[i] - 1] = i;
}
rh[0] = hs[0] = s[0];
for(int i = 1 ; i < n ; i++){
hs[i] = P * hs[i - 1] + s[i]; hs[i] %= md;
rh[i] = rh[i - 1] + tv[i] * s[i]; rh[i] %= md;
}
lcp.resize(n - 1);
for(int e = 0 ; e < n - 1 ; e++){
lcp[e] = get_lcp(sf[e] , sf[e + 1]);
}
st[0].init(n); st[1].init(n);
for(int i = 0 ; i < n ; i++){
t[i][0] = cal_t(i , 0);
a.push_back(t[i][0]);
t[i][1] = cal_t(i , 1);
}
st[0].build(a);
a.clear();
for(int i = 0 ; i < n ; i++){
a.push_back(t[i][1]);
}
st[1].build(a);
mt.init(n);
mt.build(lcp);
dv(0 , n);
cout<<ans<<'\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
palindrome.cpp:6:28: warning: bad option '-favx2' to pragma 'optimize' [-Wpragmas]
6 | #pragma GCC optimize("avx2")
| ^
palindrome.cpp:23:17: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
23 | void init(int n){
| ^
palindrome.cpp:30:53: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
30 | void build(vector<int> &a , int x , int lx , int rx){
| ^
palindrome.cpp:42:27: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
42 | void build(vector<int> &a){
| ^
palindrome.cpp:47:49: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
47 | pii cal(int l , int r , int x , int lx , int rx){
| ^
palindrome.cpp:55:23: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
55 | pii cal(int l , int r){
| ^
palindrome.cpp:66:17: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
66 | void init(int n){
| ^
palindrome.cpp:72:53: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
72 | void build(vector<int> &a , int x , int lx , int rx){
| ^
palindrome.cpp:84:27: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
84 | void build(vector<int> &a){
| ^
palindrome.cpp:89:57: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
89 | int cal(int l , int r , int k , int x , int lx , int rx){
| ^
palindrome.cpp:99:31: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
99 | int cal(int l , int r , int k){
| ^
palindrome.cpp:114:27: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
114 | int get_hash(int l , int r){
| ^
palindrome.cpp:121:33: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
121 | bool is_palindrome(int l , int r){
| ^
palindrome.cpp:130:26: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
130 | int get_lcp(int i , int j){
| ^
palindrome.cpp:145:23: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
145 | ll query(int i , int k){
| ^
palindrome.cpp:164:22: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
164 | void dv(int l , int r){
| ^
palindrome.cpp:179:25: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
179 | int cal_t(int i , bool j){
| ^
palindrome.cpp:194:10: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
194 | int main(){
| ^
# | 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... |