Submission #1018165

#TimeUsernameProblemLanguageResultExecution timeMemory
1018165jk410Palindromes (APIO14_palindrome)C++17
73 / 100
443 ms60564 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,fma") #define all(v) v.begin(),v.end() #define compress(v) v.erase(unique(all(v)),v.end()) #define lbx(v,x) lower_bound(all(v),x)-v.begin() #define ubx(v,x) upper_bound(all(v),x)-v.begin() #define maxx(a,b) a=max(a,b) #define minn(a,b) a=min(a,b) #define for0(i,n) for (int i=0; i<n; i++) #define for1(i,n) for (int i=1; i<=n; i++) #define forr(i,l,r) for (int i=l; i<=r; i++) #define sz(v) (int)v.size() using namespace std; typedef long long ll; const ll B1=1777; const ll B2=7499; const ll P1=1000000007; const ll P2=998244353; const int MAX=300000; ll PowB1[MAX+1],PowB2[MAX+1],IPowB1[MAX+1],IPowB2[MAX+1]; ll myPow(ll a,ll b,ll mod){ ll ret=1; while (b){ if (b&1) ret=ret*a%mod; a=a*a%mod; b>>=1; } return ret; } int N; string S,A; int P[MAX*2]; ll H1[MAX],H2[MAX]; map<pair<ll,ll>,int> Map; int CntHash; vector<int> Adj[MAX+1]; int L[MAX+1]; int Cnt[MAX+1]; bool Visited[MAX+1]; pair<ll,ll> getHash(int l,int r){ return {(H1[r]-(l?H1[l-1]:0)+P1)%P1*IPowB1[l]%P1,(H2[r]-(l?H2[l-1]:0)+P2)%P2*IPowB2[l]%P2}; } int getIdx(pair<ll,ll> h,int l,int r){ if (Map.find(h)!=Map.end()) return Map[h]; Map[h]=++CntHash; L[CntHash]=r-l+1; return CntHash; } void solve(int l,int r){ if (l>r) return; pair<ll,ll> h=getHash(l,r); int ih=getIdx(h,l,r); Cnt[ih]++; if (ih!=CntHash) return; while (l+1<=r-1){ pair<ll,ll> nxt=getHash(l+1,r-1); bool flag=false; if (Map.find(nxt)!=Map.end()) flag=true; int inxt=getIdx(nxt,l+1,r-1); Adj[inxt].push_back(ih); if (flag) break; h=nxt; ih=inxt; l++; r--; } } int dfs(int v){ if (Visited[v]) return Cnt[v]; Visited[v]=true; for (int i:Adj[v]) Cnt[v]+=dfs(i); return Cnt[v]; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); PowB1[0]=PowB2[0]=1; for1(i,MAX){ PowB1[i]=PowB1[i-1]*B1%P1; PowB2[i]=PowB2[i-1]*B2%P2; } IPowB1[MAX]=myPow(PowB1[MAX],P1-2,P1); IPowB2[MAX]=myPow(PowB2[MAX],P2-2,P2); for (int i=MAX; i; i--){ IPowB1[i-1]=IPowB1[i]*B1%P1; IPowB2[i-1]=IPowB2[i]*B2%P2; } cin>>S; N=sz(S); for0(i,N){ H1[i]=((i?H1[i-1]:0)+(S[i]-'a'+1)*PowB1[i]%P1)%P1; H2[i]=((i?H2[i-1]:0)+(S[i]-'a'+1)*PowB2[i]%P2)%P2; } A.push_back(S[0]); for1(i,N-1){ A.push_back('.'); A.push_back(S[i]); } for (int i=0,r=-1,k=-1; i<N*2-1; i++){ if (i<=r) P[i]=min(P[2*k-i],r-i); while (i-P[i]-1>=0&&i+P[i]+1<N*2-1&&A[i-P[i]-1]==A[i+P[i]+1]) P[i]++; if (r<i+P[i]){ r=i+P[i]; k=i; } if (i&1){ int len=(P[i]+1)/2; solve(i/2-len+1,i/2+len); } else{ int len=P[i]/2; solve(i/2-len,i/2+len); } } ll ans=0; for1(i,CntHash) maxx(ans,(ll)L[i]*dfs(i)); 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...