poj2406 Power Strings 2012-01-11

http://162.105.81.212/JudgeOnline/problem?id=2406

 

____________________________

求最长重复字串。即将字符串自我匹配,然后字符串长度减去最后一位的next值即为最长重复字串的长度。注意他求的是由多少个最长重复字串组成。

____________________________

 1 Program stone;
 2 var i,j:longint;
 3     s:ansistring;
 4     b:array[1..1000005]of longint;
 5 Begin
 6  assign(input,'input.in');reset(input);
 7   readln(s);
 8   while s<>'.' do
 9    begin
10      j:=0;
11      b[1]:=0;
12      for i:=2 to length(s) do
13       begin
14         while (j>0)and(s[i]<>s[j+1]) do j:=b[j];
15         if s[j+1]=s[i] then inc(j);
16         b[i]:=j;
17       end;
18      i:=length(s);
19      if i mod (i-b[i])=0 then writeln(i div(i-b[i])) else writeln(1);
20      readln(s);
21    end;
22 end.
23 
24  

 

posted on 2016-03-02 20:44  Yesphet  阅读(116)  评论(0编辑  收藏  举报