POJ 2406(KMP中next的性质）

Power Strings
 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 24403 Accepted: 10264

Description

Input

Output

Sample Input

abcd
aaaa
ababab
.


Sample Output

1
4
3


Hint

Source

a b c a  b  c

0 0 0 1  2  3

abcabcabc

000123456

a a i a a i a a i

0 10 1 2 3 4 5 6

于是发现next函数的嵌套关系：

L1-L2

L1-L2 L3-L4

Program PowerString;
const
maxn=10000010;
var
n,i,j,duan_luo:longint;
next:array[0..maxn] of longint;
a,b:ansistring;
function check:boolean;
var
i,j:longint;
begin
if (n mod duan_luo>0) then exit(false);
for i:=duan_luo+1 to n do if (a[i]<>a[i-duan_luo]) then exit(false);
exit(true);

end;
begin
while not eof do
begin
if a='.' then break;

n:=length(a);
j:=0; i:=1; next[1]:=0;
while (i<n) do
begin
if (j=0) or (a[i]=a[j]) then
begin
inc(i);inc(j);
if (a[i]<>a[j]) then next[i]:=next[j]
else next[i]:=j;
end
else j:=next[j];

end;

duan_luo:=n-next[i];
if check then writeln(n div duan_luo) else writeln(1);

end;
end.


Program PowerString;
const
maxn=10000010;
var
n,i,j,duan_luo:longint;
next:array[0..maxn] of longint;
a,b:ansistring;
function check:boolean;
var
i,j:longint;
begin
if (n mod duan_luo>0) then exit(false);
for i:=duan_luo+1 to n do if (a[i]<>a[i-duan_luo]) then exit(false);
exit(true);

end;
begin
while not eof do
begin
if a='.' then break;

n:=length(a);
j:=0; i:=1; next[1]:=0;
while (i<n) do
begin
if (j=0) or (a[i]=a[j]) then
begin
inc(i);inc(j);
//    if (a[i]<>a[j]) then next[i]:=next[j]
while (j>0) and (a[i]<>a[j]) do j:=next[j];
next[i]:=j;
end
else j:=next[j];

end;

duan_luo:=n-next[i];
if check then writeln(n div duan_luo) else writeln(1);

end;
end.


Program PowerString;
const
maxn=10000010;
var
n,i,j,duan_luo:longint;
next:array[0..maxn] of longint;
a,b:ansistring;
function check:boolean;
var
i,j:longint;
begin
if (n mod duan_luo>0) then exit(false);
for i:=duan_luo+1 to n do if (a[i]<>a[i-duan_luo]) then exit(false);
exit(true);

end;
begin
while not eof do
begin
if a='.' then break;

n:=length(a);
j:=0; i:=1; next[1]:=0;
while (i<n) do
begin
inc(i);inc(j);
//if (a[i]<>a[j]) then next[i]:=next[j]
while (j>0) and (a[i]<>a[j]) do j:=next[j];
next[i]:=j;

end;

duan_luo:=n-next[i];
if check then writeln(n div duan_luo) else writeln(1);

end;
end.

Program PowerString;
const
maxn=10000010;
var
n,i,j,duan_luo:longint;
next:array[0..maxn] of longint;
a,b:ansistring;
function check:boolean;
var
i,j:longint;
begin
if (n mod duan_luo>0) then exit(false);
for i:=duan_luo+1 to n do if (a[i]<>a[i-duan_luo]) then exit(false);
exit(true);

end;
begin
while not eof do
begin
if a='.' then break;

n:=length(a);
j:=0; i:=1; next[1]:=0;
for i:=2 to n do
begin
inc(j);
//if (a[i]<>a[j]) then next[i]:=next[j]
while (j>0) and (a[i]<>a[j]) do j:=next[j];
next[i]:=j;
end;

duan_luo:=n-next[i];
if check then writeln(n div duan_luo) else writeln(1);

end;
end.