Advertisement
yicongli

LG3804

Mar 7th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define gc c=getchar()
  6. #define r(x) read(x)
  7. #define ll long long
  8.  
  9. template<typename T>
  10. inline void read(T&x){
  11.     x=0;T k=1;char gc;
  12.     while(!isdigit(c)){if(c=='-')k=-1;gc;}
  13.     while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
  14. }
  15.  
  16. const int N=2e6+7;
  17.  
  18. int Max[N];
  19. int Min[N];
  20. int to[N][26];
  21. int fa[N];
  22. int siz[N];
  23.  
  24. int tot;
  25.  
  26. inline int NewNode(int mx,int mi,int *tr,int su){
  27.     ++tot;
  28.     Max[tot]=mx;
  29.     Min[tot]=mi;
  30.     if(tr!=NULL)memcpy(to[tot],tr,26<<2);
  31.     fa[tot]=su;
  32.     return tot;
  33. }
  34.  
  35. inline int extend(int u,int c){
  36.     int t=NewNode(Max[u]+1,0,NULL,0);
  37.     siz[t]=1;
  38.     for(;u&&!to[u][c];u=fa[u])to[u][c]=t;
  39.     if(!u){
  40.         Min[t]=1;
  41.         fa[t]=1;
  42.         return t;
  43.     }
  44.     int v=to[u][c];
  45.     if(Max[v]==Max[u]+1){
  46.         Min[t]=Max[v]+1;
  47.         fa[t]=v;
  48.         return t;
  49.     }
  50.     int x=NewNode(Max[u]+1,0,to[v],fa[v]);
  51.     Min[t]=Min[v]=Max[x]+1;
  52.     fa[t]=fa[v]=x;
  53.     Min[x]=Min[fa[x]]+1;
  54.     for(;u&&to[u][c]==v;u=fa[u])to[u][c]=x;
  55.     return t;
  56. }
  57.  
  58. char s[N];
  59. int deg[N];
  60. int ans[N];
  61. queue<int>Q;
  62.  
  63. int main(){
  64.     scanf("%s",s);
  65.     int n=strlen(s);
  66.     int lst=NewNode(0,0,NULL,0);
  67.     for(int i=0;i<n;++i){
  68.         lst=extend(lst,s[i]-'a');
  69.     }
  70.     for(int i=2;i<=tot;++i){
  71.         deg[fa[i]]++;
  72.     }
  73.     for(int i=1;i<=tot;++i){
  74.         if(!deg[i]){
  75.             Q.push(i);
  76.         }
  77.     }
  78.     while(!Q.empty()){
  79.         int x=Q.front();Q.pop();
  80.         siz[fa[x]]+=siz[x];
  81.         if(fa[x]&&!--deg[fa[x]]){
  82.             Q.push(fa[x]);
  83.         }
  84.     }
  85.     ll ans=0;
  86.     for(int i=2;i<=tot;++i){
  87.         if(siz[i]>1){
  88.             ans=max(ans,(ll)siz[i]*Max[i]);
  89.         }
  90.     }
  91.     printf("%lld\n",ans);
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement