标签:c style class blog code tar
写了好久,不知道为什么不过,也不清楚到底卡在哪里。。。
只好看别人的代码,感觉除了HASH不一样外,倒没什么特别之处。同时参考那论文写的。。
http://blog.csdn.net/jyysc2010/article/details/9964513
1 #include<stdio.h> 2 #include<cstring> 3 #include<cstdlib> 4 #define Min(a,b) (a<b?a:b) 5 struct node 6 { 7 int x,y; 8 } hash[250008],a1,a2; 9 int p=4877; 10 int n,m,f[505][505],ans,x1,x2,y1,y2,a[505][505],g[505][505],b[250008],next[250008],start[530000],tot; 11 long long s[505]; 12 13 void insert(int x,node t)//将HASH值x插入链表 14 { 15 hash[++tot]=t; 16 next[tot]=start[x]; 17 start[x]=tot; 18 } 19 20 bool cmp(int x,int l)//比较HASH值为X的相同矩阵是否出现过 21 { 22 int i,j; 23 for (int g=start[x]; g; g=next[g]) 24 { 25 a2=hash[g]; 26 j=l; 27 for (i=0; i<l&&j>=l; i++) 28 if (f[a1.x][a1.y-i]!=f[a2.x][a2.y-i]) break; 29 if (i>=l) 30 { 31 if (l>ans) ans=l,x1=a1.x,y1=a1.y,x2=a2.x,y2=a2.y; 32 return true; 33 } 34 } 35 return false; 36 } 37 38 int main() 39 { 40 int i,j,k,l,r; 41 char c; 42 scanf("%d%d%*c",&n,&m); 43 for (i=1; i<=n; i++) 44 { 45 for (j=1; j<=m; j++) 46 { 47 scanf("%c",&a[i][j]); 48 a[i][j]-=97; 49 } 50 scanf("%*c"); 51 } 52 s[0]=1; 53 for (i=1; i<=n; i++) s[i]=(p*s[i-1])&524287; 54 55 r=Min(n,m); 56 l=1; 57 while (l<=r) 58 { 59 k=l+r>>1; 60 for (i=1; i<=n; i++) 61 for (j=1; j<=m; j++) 62 { 63 if (i<k) f[i][j]=f[i-1][j]*p+a[i][j]&524287; 64 else f[i][j]=((f[i-1][j]-a[i-k][j]*s[k-1]&524287)+524288&524287)*p+a[i][j]&524287;//减去最高位,加上最低位 65 66 }//f[i][j]存下第j列第i行往上长度为k的串的HASH值 67 memset(start,0,sizeof(start)); 68 tot=0; 69 for (j=1; j<=m&&i>n; j++) 70 for (i=k; i<=n; i++) 71 { 72 if (j<k) g[i][j]=g[i][j-1]*p+f[i][j]&524287; 73 else 74 { 75 g[i][j]=((g[i][j-1]-f[i][j-k]*s[k-1]&524287)+524288&524287)*p+f[i][j]&524287; 76 if (cmp(g[a1.x=i][a1.y=j],k)) break; 77 insert(g[i][j],a1); 78 } 79 }//g[i][j]存下右下角为第i行第j列长度为k的矩阵的HASH值 80 if (j<=m) l=k+1; 81 else r=k-1; 82 83 } 84 if (ans) printf("%d\n%d %d\n%d %d\n",ans,x1-ans+1,y1-ans+1,x2-ans+1,y2-ans+1); 85 else printf("0\n"); 86 return 0; 87 }
标签:c style class blog code tar
原文地址:http://www.cnblogs.com/jie-dcai/p/3761844.html