跳到正文

剪贴板

剪贴板 674yryl1

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
#define mid ((l+r)>>1)
#define int long long
using namespace std;
const int N=3e5+10,mod=1e9+7,INF=0x3f3f3f3f3f3f3f3f,lim=19;
int n,m=127,ans,c[N],sa[N],rk[N],old[N],rkk[N],h[N],st[20][N],lg[N];char s[N],t[N<<1];
namespace Fast_IO
{
    static char buf[1000000],*paa=buf,*pd=buf,out[10000000];int length=0;
    #define getchar() paa==pd&&(pd=(paa=buf)+fread(buf,1,1000000,stdin),paa==pd)?EOF:*paa++
    inline int read()
    {
        int x(0),t(1);char fc(getchar());
        while(!isdigit(fc)){if(fc=='-') t=-1;fc=getchar();}
        while(isdigit(fc)) x=(x<<1)+(x<<3)+(fc^48),fc=getchar();
        return x*t;
    }
    inline void flush(){fwrite(out,1,length,stdout);length=0;}
    inline void put(char c){if(length==9999999) flush();out[length++]=c;}
    inline void put(string s){for(char c:s) put(c);}
    inline void print(int x)
    {
        if(x<0) put('-'),x=-x;
        if(x>9) print(x/10);
        put(x%10+'0');
    }
    inline bool chk(char c) { return !(c>='a'&&c<='z'); }
    inline bool ck(char c) { return c!='\n'&&c!='\r'&&c!=-1&&c!=' '; }
    inline void rd(char s[],int&n)
    {
        s[++n]=getchar();
        while(chk(s[n])) s[n]=getchar();
        while(ck(s[n])) s[++n]=getchar();
        n--;
    }
}
using namespace Fast_IO;
namespace SA
{
    inline void get_SA(int n,int sa[],int rk[],int old[],int c[],char s[])
    {
        for(int i=1;i<=n;i++) ++c[rk[i]=s[i]];
        for(int i=2;i<=m;i++) c[i]+=c[i-1];
        for(int i=n;i>=1;i--) sa[c[rk[i]]--]=i;
        for(int k=1;k<=n;k<<=1)
        {
            int tot=0;
            for(int i=n-k+1;i<=n;i++) old[++tot]=i;
            for(int i=1;i<=n;i++) if (sa[i]>k) old[++tot]=sa[i]-k;
            for(int i=1;i<=m;i++) c[i]=0;
            for(int i=1;i<=n;i++) c[rk[i]]++;
            for(int i=2;i<=m;i++) c[i]+=c[i-1];
            for(int i=n;i>=1;--i) sa[c[rk[old[i]]]--]=old[i],old[i]=0;
            swap(rk,old);rk[sa[1]]=1;tot=1;
            for(int i=2;i<=n;i++) rk[sa[i]]=(old[sa[i]]==old[sa[i-1]]&&old[sa[i]+k]==old[sa[i-1]+k])?tot:++tot;
            if(tot==n) break;m=tot;
        }
    }
    inline void get_height(int n,int h[],int rk[],char s[])
    {
        int k=0;
        for(int i=1;i<=n;i++) rk[sa[i]]=i;
        for(int i=1,j;i<=n;i++)
        {
            if(rk[i]==1) continue;if(k) k--;j=sa[rk[i]-1];
            while(j+k<=n&&i+k<=n&&s[i+k]==s[j+k]) k++;
            h[rk[i]]=k;
        }
    }
}
using namespace SA;
inline int lcs(int x,int y){if(x^y){int i=log2(y-x);return min(st[i][x+1],st[i][y-(1<<i)+1]);}return INF;}
inline void calc(int x,int y)
{
    x=(x+1)/2,y>>=1;int pos=rkk[x],res=1,l=0,r=n-pos,cur=0;
    while(l<=r)
        if(lcs(pos,pos+mid)>=y-x+1) cur=mid,l=mid+1;
        else r=mid-1;
    res+=cur;l=0,r=pos-1;
    while(l<=r)
        if(lcs(pos-mid,pos)>=y-x+1) cur=mid,l=mid+1;
        else r=mid-1;
    res+=cur;ans=max(ans,res*(y-x+1));
}
namespace Manacher
{
    inline void get(int n,char s[],char t[])
    {
        t[0]=t[1]='#';for(int i=1;i<=n;i++) t[i<<1]=s[i],t[i<<1|1]='#';
    }
    inline void manacher(char s[],int h[],int n)
    {
        int r=0,md=1;
        for(int i=1;i<n;i++)
        {
            h[i]=i<=r?min(h[md*2-i],r-i):1;
            while(s[i+h[i]]==s[i-h[i]]){h[i]++;if(h[i]+i>r) calc(i-h[i]+1,i+h[i]-1);}
            if(h[i]+i>r) r=h[i]+i,md=i;
        }
    }
}
using namespace Manacher;
signed main()
{
    rd(s,n);get_SA(n,sa,rk,old,c,s);get_height(n,h,rkk,s);
    for(int i=1;i<=n;i++) st[0][i]=h[i];
    for(int j=1;j<=lim;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
            st[j][i]=min(st[j-1][i],st[j-1][i+(1<<(j-1))]);
    get(n,s,t);manacher(t,h,n<<1|1);print(ans);
    flush();return 0;
}