There is a string with a length of less than 1000000. Calculate the maximum number of chunks, that is, express s as s '+ s '······. Calculate the number of s.
Use the KMP idea to find the next array, and then use the hand-drawn method to find that the strings from next [length (s)] to length (s) can be pushed forward continuously, as long as the length can be divisible by the total length, it is the maximum length of the chain, otherwise it is 1.
View code
1 program pku2406 (input, output );
2 VaR
3 I, j: longint;
4 S: ansistring;
5 next: array [0 .. 1200000] of longint;
6 begin
7 readln (s );
8 while S <> '.' Do
9 begin
10 J: = 0;
11 next [1]: = 0;
12 For I: = 2 to length (s) do
13 begin
14 While (s [I] <> S [J + 1]) and (j> 0) Do
15 J: = next [J];
16 if s [I] = s [J + 1] Then
17 Inc (j );
18 next [I]: = J;
19 end;
20 if (length (s) mod (length (S)-next [length (s)]) = 0) then
21 writeln (length (s) Div (length (S)-next [length (s)])
22 else
23 writeln (1 );
24 readln (s );
25 end;
26 end.