Pku2406 power strings

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.