Submission #388116

#TimeUsernameProblemLanguageResultExecution timeMemory
388116KeshiPalindromes (APIO14_palindrome)C++17
73 / 100
1033 ms59176 KiB
//In the name of God #include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; typedef int ll; typedef pair<ll, ll> pll; const ll maxn = 3e5 + 100; const ll lg = 19; const ll mod = 1e9 + 7; const ll inf = 1e9; #define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout); #define pb push_back #define Mp make_pair #define F first #define S second #define Sz(x) ll((x).size()) #define all(x) (x).begin(), (x).end() #define lc (id << 1) #define rc (lc | 1) ll n, p[maxn], rnk[maxn], lcp[maxn], f[maxn]; string s; vector<ll> vec[maxn]; ll mn[lg][maxn]; void bld(){ for(ll i = 0; i < n + 1; i++){ mn[0][i] = lcp[i]; } for(ll i = 1; i < lg; i++){ for(ll j = 0; j + (1 << i) <= n + 1; j++){ mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << (i - 1))]); } } } ll get(ll l, ll r){ if(l == r) return inf; ll t = r - l; ll i = f[t]; return min(mn[i][l], mn[i][r - (1 << i)]); } int main(){ fast_io; for(ll i = 2; i < maxn; i++){ f[i] = f[i / 2] + 1; } cin >> s; n = Sz(s); s += '#'; vector<int> sa, rev; sa.resize(n+1); iota(sa.begin(),sa.end(),0); sort(sa.begin(),sa.end(), [&](int a,int b){ if(s[a]==s[b]) return a>b; return s[a]<s[b]; }); vector<int> c(n+1,0),r(n+1),cnt(n+1); for(int i=0;i<=n;i++) r[i]=s[i]; for(int len=1;len<=n;len*=2){ for(int i=0;i<=n;i++){ c[sa[i]]=i; if(i>0 && r[sa[i-1]]==r[sa[i]] && sa[i-1]+len<=n && r[sa[i-1]+len/2]==r[sa[i]+len/2]) c[sa[i]]=c[sa[i-1]]; } iota(cnt.begin(),cnt.end(),0); copy(sa.begin(),sa.end(),r.begin()); for(int i=0;i<=n;i++){ int s1=r[i]-len; if(s1>=0) sa[cnt[c[s1]]++]=s1; } c.swap(r); } rev.resize(n+1); for(int i=0;i<=n;i++) rev[sa[i]]=i; for(ll i = 0; i < n + 1; i++){ p[i] = sa[i]; rnk[i] = rev[i]; } for(ll i = 0, k = 0; i < n + 1; i++){ if(rnk[i] == n){ k = 0; continue; } ll j = p[rnk[i] + 1]; while(i + k < n + 1 && j + k < n + 1 && s[i + k] == s[j + k]) k++; lcp[rnk[i]] = k; if(k) k--; } bld(); vector<int> d1(n); for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } vector<int> d2(n); for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1); while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) { k++; } d2[i] = k--; if (i + k > r) { l = i - k - 1; r = i + k; } } set<ll> st; long long ans = 0; for(ll i = n; i--;){ for(ll j : vec[i]){ st.erase(j); } vec[i].clear(); st.insert(i); vec[i - d1[i]].pb(i); auto it = st.rbegin(); while(it != st.rend()){ ll t = (*it - i) * 2 + 1; if(t <= lcp[rnk[i] - 1]) break;; ll l = rnk[i], r = n + 1; while(r - l > 1){ ll mid = (l + r) >> 1; if(get(rnk[i], mid) >= t) l = mid; else r = mid; } ans = max(ans, ((long long)t) * (r - rnk[i])); it++; } } st.clear(); for(ll i = n - 1; i--;){ for(ll j : vec[i]){ st.erase(j); } if(d2[i + 1]) st.insert(i); if(d2[i + 1]) vec[i - d2[i + 1]].pb(i); if(st.empty()) continue; auto it = st.rbegin(); while(it != st.rend()){ ll t = (*it - i) * 2 + 2; if(t <= lcp[rnk[i] - 1]) break; ll l = rnk[i], r = n + 1; while(r - l > 1){ ll mid = (l + r) >> 1; if(get(rnk[i], mid) >= t) l = mid; else r = mid; } ans = max(ans, ((long long)t) * (r - rnk[i])); it++; } } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...