POJ 1961 Period (KMP seeking period)

Source: Internet
Author: User

Period
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 15963 Accepted: 7644

Description

For each prefix of a given string S with N characters (each character have an ASCII code between and 126, inclusive), we Want to know whether the prefix is a periodic string. That's, for each I (2 <= i <= N) we want to know the largest K > 1 (if there are one) such that the prefix of S W ith length I can be written as AK, which is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of lines. The first one contains n (2 <= n <= 1 000 000), haven size of the string s.the second line contains the string S. The Input file ends with a line, have the
Number zero on it.

Output

For each test case, output ' test Case # ' and the consecutive test case number on a '; Then, for each prefix with length I that have a period K > 1, output the prefix size I and the period K separated by a s Ingle Space; The prefix sizes must is in increasing order. Print a blank line after each test case.

Sample Input

3aaa12aabaabaabaab0

Sample Output

Test Case #12 3Test case #22 26 29 312 4
Test instructions: Multiple sets of test data, the first line of N, indicating the length of the string to be entered next, enter 0 o'clock to end the input.

Thinking of solving problems: This problem may just start to look at the time do not know what to get, I also looked at a long time to understand, only blame me too food ah .... In fact, it is the string in which to ask for inputa substring at any length, as long as the substring has an integer number of cycles (both cyclic sections), the length and number of cycles of the substring are output at this time;

For example, the second set of test data:
String Aabaabaabaab
When the length is 2 o'clock, the substring is AA, the substring has two cycles, each cycle is a (with the cycle section, each cycle section is a, no longer described);
When the length is 6 o'clock, the substring is Aabaab and has two cycles, each period is aab;
When the length is 9 o'clock, the substring is AABAABAAB and has three cycles, each period is aab;
When the length is 12 o'clock, the substring is Aabaabaabaab, with four cycles, each with a aab.
End.
Where the length of the other substring is not satisfied with the whole number of cycles.
Specific code: #include <stdio.h> #include <string.h>char s[1000005]; int next[1000005];void make_next (int len) {int I=0,j=-1;memset (next,0,sizeof (next)); Next[0]=-1;while (I<len) {if (j ==-1 | | S[i]==s[j]) {i++; j++;next[i]=j;} ELSEJ=NEXT[J];}} int main () {int len,min_repetend,k=0;while (scanf ("%d", &len) && len!=0) {scanf ("%s", s); Make_next (len);p rintf ("Test case #%d\n", ++k); for (int i=1;i<=len;i++)//Traverse all string lengths to satisfy the condition of the substring {min_repetend=i-next[ i];//The string length of I for the period if (i%min_repetend==0 && next[i]!=0) printf ("%d%d\n", i,i/min_repetend);} printf ("\ n");} return 0;}


POJ 1961 Period (KMP seeking period)

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.