pku2185 Milking Grid 2012-01-11
http://poj.org/problem?id=2185_
_____________________________
我的做法是错的,但它可以过- -正确的做法应该是用扩展kmp,
______________________________
1 Program stone; 2 var i,j,k,n,m,lc,rc:longint; 3 s:array[1..10000,1..100]of char; 4 b:array[1..10000,1..100]of longint; 5 function gcd(x,y:longint):longint; 6 var i,j,k:longint; 7 begin 8 if x>y then begin i:=x;j:=y;end 9 else begin i:=y;j:=x;end; 10 while i mod j<>0 do 11 begin 12 k:=i-j; 13 if k<j then begin i:=j;j:=k;end 14 else i:=k; 15 end; 16 gcd:=x*y div j; 17 end; 18 Begin 19 assign(input,'input.in');reset(input); 20 readln(n,m); 21 for i:=1 to n do 22 begin 23 for j:=1 to m do read(s[i,j]); 24 readln; 25 end; 26 lc:=1;rc:=1; 27 for i:=1 to n do 28 begin 29 k:=0;b[i,1]:=0; 30 for j:=2 to m do 31 begin 32 while (k>0)and(s[i,j]<>s[i,k+1]) do k:=b[i,k]; 33 if s[i,j]=s[i,k+1] then inc(k); 34 b[i,j]:=k; 35 end; 36 k:=m-b[i,m]; 37 lc:=gcd(lc,k); 38 end; 39 if lc>m then lc:=m; 40 fillchar(b,sizeof(b),0); 41 for i:=1 to m do 42 begin 43 k:=0;b[1,i]:=0; 44 for j:=2 to n do 45 begin 46 while (k>0)and(s[j,i]<>s[k+1,i]) do k:=b[k,i]; 47 if s[j,i]=s[k+1,i] then inc(k); 48 b[j,i]:=k; 49 end; 50 k:=n-b[n,i]; 51 rc:=gcd(rc,k); 52 end; 53 if rc>m then rc:=m; 54 write(lc*rc); 55 end. 56 57
_____MildTheorem