1 solutions

  • 0
    @ 2026-7-29 11:15:49
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int n;
    string s[5005],t;
    int a[4005][4005];
    int f[10005];
    int b[10005],c[10005];
    inline int calc(int x,int y,int op){return min(n-x-1,y)*2+op;}
    void Main() {
    	cin>>n;
    	REP(i,0,n)cin>>s[i];
    	t=s[0];
    	REP(delta,-(n-1),n){
    		//i-j=delta
    		int l=0,r=n-1;
    		l=max(l,delta);//i-deleta>=0
    		r=min(r,n+delta-1);//i-delta<n
    		REP(i,l,r+1)t[i]=s[i][i-delta];
    		f[l]=0;
    		int L=l,R=l;
    		REP(i,l+1,r+1){
    			int x=0;
    			if(i<=R)x=min(R-i,f[L+R-i]);
    			while(i+x+1<=r&&i-x-1>=l&&t[i-x-1]==t[i+x+1])++x;
    			f[i]=x;
    			if(i+x>=R)R=i+x,L=i-x;
    		}
    		//最远匹配到 i+f[i]
    		REP(i,l,r+1)a[2*i-delta][calc(i,i-delta,0)]=f[i];
    		f[l]=0;L=l,R=l-1;
    		REP(i,l+1,r+1){
    			int x=0;
    			if(i<=R)x=min(R-i+1,f[L+R-i+1]);
    			while(i+x<=r&&i-x-1>=l&&t[i-x-1]==t[i+x])++x;
    			f[i]=x;
    			if(i+x-1>=R)R=i+x-1,L=i-x;
    		}
    		//匹配长度是 f[i]
    		REP(i,l+1,r+1)a[2*i-delta-1][calc(i,i-delta-1,1)]=f[i];
    	}
    	int ans=0,sum=0;
    	REP(i,0,2*n-1){
    		int m=min(i,2*n-i-2)+1;
    		m=2*m-1;
    		REP(j,0,m)b[j]=0,f[j]=a[i][j];
    //		REP(j,0,m)cout<<f[j]<<' ';
    //		cout<<endl;
    		int x=0;
    		for(int p=0;p<m;p+=2){
    			while(x+2<m&&min(f[x+1],f[x+2])-(x+2-p)/2>=0)x+=2,b[x]=p;
    		}
    		x=m-1;c[x]=m-1;
    		for(int p=m-1;p>=0;p-=2){
    			while(x-2>=0&&min(f[x-1],f[x-2])-(p-x+2)/2>=0)x-=2,c[x]=p;
    		}
    		//边长为 2x+1:
    		//0 1 1 2 2 ... x-1  x-1  x    x   x  x-1 x-1 ... 1 1 0
    		//p 1 2        2x-3 2x-2 2x-1 2x 2x+1
    		//min(f[i]-(i+1)/2)>=-p/2  p=y-2x
    		for(int p=0;p<m;p+=2){
    			int mx=min(p-b[p],c[p]-p)/2;
    //			cout<<'!'<<i<<' '<<p<<' '<<mx<<' '<<c[p]<<endl;
    			sum+=mx+1;ans=max(ans,mx*2+1);
    		}
    		//边长为 2x
    		for(int p=1;p<m;p+=2){
    			int mx=min(p-1-b[p-1],c[p+1]-p-1)/2+1;mx=min(mx,f[p]);
    //			cout<<'!'<<i<<' '<<p<<' '<<mx<<endl;
    			sum+=mx;ans=max(ans,mx*2);
    		}
    	}
    	cout<<ans<<endl;
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("matrix.in","r",stdin);
    	freopen("matrix.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    3084
    Time
    6000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By