poj 2185 (KMP)

完全不会啊……

附一份题解:http://blog.sina.com.cn/s/blog_69c3f0410100tyjl.html

 1 var i,j,k,r,c,x:longint;
 2     ch:array[0..10100,0..80] of char;
 3     s:string;
 4     rr,cc:array[0..10100] of boolean;
 5     pre:array[0..10100] of longint;
 6 procedure init;
 7  begin
 8  readln(r,c);
 9  for i:=1 to r do
10   begin
11    readln(s);
12    for j:=1 to c do ch[i,j]:=s[j];
13   end;
14  end;
15 procedure main;
16  begin
17  fillchar(rr,sizeof(rr),true);
18  fillchar(cc,sizeof(cc),true);
19  for x:=1 to r do
20   begin
21    pre[1]:=0;k:=0;
22    for i:=2 to c do
23     begin
24      while (k<>0) and (ch[x,k+1]<>ch[x,i]) do k:=pre[k];
25      if ch[x,k+1]=ch[x,i] then inc(k);
26      pre[i]:=k;
27     end;
28    k:=c;
29    for i:=0 to c do
30     if c-pre[k]<>i then cc[i]:=false else k:=pre[k];
31   end;
32  for x:=1 to c do
33   begin
34    pre[1]:=0;k:=0;
35    for i:=2 to r do
36     begin
37      while (k<>0) and (ch[k+1,x]<>ch[i,x]) do k:=pre[k];
38      if ch[k+1,x]=ch[i,x] then inc(k);
39      pre[i]:=k;
40     end;
41    k:=r;
42    for i:=0 to r do
43     if r-pre[k]<>i then rr[i]:=false else k:=pre[k];
44   end;
45   for i:=1 to r do if rr[i] then break;
46   for j:=1 to c do if cc[j] then break;
47   writeln(i*j);
48   end;
49 begin
50  init;
51  main;
52 end.             
View Code

  if c-pre[k]<>i then cc[i]:=false else k:=pre[k];

  if r-pre[k]<>i then rr[i]:=false else k:=pre[k];

这两句话到底什么意思?

 

posted @ 2014-06-06 19:35  ZYF-ZYF  Views(187)  Comments(0Edit  收藏  举报