Submission #356817

#TimeUsernameProblemLanguageResultExecution timeMemory
356817KerimPalindromes (APIO14_palindrome)C++17
0 / 100
574 ms44624 KiB
#include "bits/stdc++.h" #define MAXN 300009 #define INF 1000000007 #define mp(x,y) make_pair(x,y) #define all(v) v.begin(),v.end() #define pb(x) push_back(x) #define wr cout<<"----------------"<<endl; #define ppb() pop_back() #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++) #define ff first #define ss second #define my_little_dodge 46 #define debug(x) cerr<< #x <<" = "<< x<<endl; using namespace std; typedef long long ll; typedef pair<int,int> PII; template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;} const int LOG=18; int S[MAXN][LOG],c[MAXN],ord[MAXN],lcp[MAXN],rad[MAXN<<1]; PII C[MAXN]; char s[MAXN],tmp[MAXN<<1]; bool cmp(int x,int y){ return (C[x]<C[y]); } int ata[MAXN],sz[MAXN]; int tap(int x){ if(ata[x]==x)return x; return ata[x]=tap(ata[x]); } void merge(int x,int y){ if((x=tap(x))==(y=tap(y))) return; ata[y]=x; } int main(){ //freopen("file.in", "r", stdin); scanf("%s",s+1); int n=strlen(s+1); for(int i=1;i<=n;i++) S[i][0]=s[i]-'a'+1; for(int j=1;j<LOG;j++){ for(int i=1;i<=n;i++) C[i]=mp(S[i][j-1],(i+(1<<(j-1))<=n?S[i+(1<<(j-1))][j-1]:-1)),c[i]=i; sort(c+1,c+n+1,cmp); for(int i=1;i<=n;i++) S[c[i]][j]=S[c[i-1]][j]+(C[c[i]]!=C[c[i-1]]); } for(int i=1;i<=n;i++) ord[S[i][LOG-1]]=i; vector<pair<int,PII> >v; vector<PII>g; for(int i=1;i<n;i++){ int a=ord[i],b=ord[i+1]; for(int j=LOG-1;j>=0;j--) if(a<=n and b<=n and S[a][j]==S[b][j]) a+=(1<<j),b+=(1<<j),lcp[i]+=(1<<j); v.pb(mp(lcp[i],mp(ord[i],ord[i+1]))); } sort(all(v),greater<pair<int,PII>>()); for(int i=0;i<2*n+1;i++)tmp[i]='#'; for(int i=0;i<n;i++)tmp[i*2+1]=s[i+1]; //printf("%s\n",tmp); for(int i=0,j=0,k;i<n*2+1;i+=k){ while(i-j>=0&&i+j<n*2+1&&tmp[i-j]==tmp[i+j])j++; rad[i]=j;k=1; while(rad[i-k]<rad[i]-k)rad[i+k]=rad[i-k],k++; j=max(0,j-k); } //odd for(int i=1;i<=n;i++)g.pb(mp(rad[i*2-1]/2,i)); sort(all(g),greater<PII>()); int p=0;ll ans=0; for(int i=1;i<=n;i++)ata[i]=i,sz[i]=0; for(int i=0;i<n;i++){ while(p<int(v.size()) and v[p].ff>=g[i].ff) merge(v[p].ss.ff,v[p].ss.ss),p++; sz[tap(g[i].ss)]++; umax(ans,sz[tap(g[i].ss)]*1LL*(g[i].ff*2-1)); } //even g.clear(); for(int i=1;i<n;i++)g.pb(mp(rad[i*2]/2,i+1)); sort(all(g),greater<PII>());p=0; for(int i=1;i<=n;i++)ata[i]=i,sz[i]=0; for(int i=0;i<n-1;i++){ while(p<int(v.size()) and v[p].ff>=g[i].ff) merge(v[p].ss.ff,v[p].ss.ss),p++; sz[tap(g[i].ss)]++; umax(ans,sz[tap(g[i].ss)]*1LL*(g[i].ff*2)); } printf("%lld\n",ans); /*for(int i=0;i<2*n+1;i++) printf("%d ",rad[i]); puts("");*/ return 0; }

Compilation message (stderr)

palindrome.cpp: In function 'int main()':
palindrome.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   39 |     scanf("%s",s+1);
      |     ~~~~~^~~~~~~~~~
#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...