First of all, for each position, it starts the hash of the length of Y, and then for the hash value of the vertical column of the hash value, sorted after the outstanding number.
Time Complexity $o (n^2\log N) $.
#include <cstdio> #include <algorithm> #define N 1010typedef unsigned long long ll;const ll D1=197,d2=131;int N , M,x,y,i,j,ans,t,cnt;char a[n][n];ll pow1[n],pow2[n],h[n][n],tmp;struct p{int x,y;ll z; P () {}p (int _x,int _y,ll _z) {x=_x,y=_y,z=_z;}} q[n*n],fin[n];inline BOOL CMP (const P&a,const p&b) {return a.z<b.z;} inline bool Cmp2 (const P&a,const p&b) {return a.x==b.x?a.y<b.y:a.x<b.x;} int main () {scanf ("%d%d", &n,&m), gets (a[0]); for (i=1;i<=n;i++) gets (a[i]+1); scanf ("%d%d", &x,&y); for (pow1[0]=pow2[0]=i=1;i<=n| | i<=m;i++) Pow1[i]=pow1[i-1]*d1,pow2[i]=pow2[i-1]*d2; for (i=1;i<=n;i++) {for (tmp=0,j=1;j<y;j++) tmp=tmp*d1+a[i][j],h[i][j]=0; for (j=y;j<=m;j++) h[i][j]=tmp=tmp*d1-pow1[y]*a[i][j-y]+a[i][j]; } for (t=0,i=y;i<=m;i++) {for (tmp=0,j=1;j<x;j++) tmp=tmp*d2+h[j][i]; for (j=x;j<=n;j++) q[++t]=p (J-x+1,i-y+1,tmp=tmp*d2-pow2[x]*h[j-x][i]+h[j][i]); } for (Std::sort (q+1,q+t+1,cmp), j=1,i=2;i<=t;i++) if (q[i-1].z!=q[I].Z) {if (I-j>ans) ans=i-j,tmp=q[j].z; J=i; } if (T-j+1>ans) ans=t-j+1,tmp=q[t].z; printf ("%d%d\n", x, y); for (i=1;i<=t;i++) if (q[i].z==tmp) fin[++cnt]=p (q[i].x,q[i].y,0); Std::sort (FIN+1,FIN+CNT+1,CMP2); For (I=0;i<x;puts ("), i++) for (j=0;j<y;j++) Putchar (A[fin[1].x+i][fin[1].y+j]); For (printf ("%d\n", CNT), i=1;i<=cnt;i++) printf ("%d%d\n", fin[i].x,fin[i].y); return 0;}
BZOJ1170: [Balkan2007]cipher