HDU 1358 Period The prefix length and number of occurrences (the use of the next array of KMP)

Source: Internet
Author: User

Period

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 12428 Accepted Submission (s): 5825


Problem descriptionfor Each prefix of a given string S with N characters (each character have an ASCII code between 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.

Inputthe input file 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, has the number zero on it.

Outputfor 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 input3aaa12aabaabaabaab0

Sample outputtest Case #12 3Test case #22 26 29 312 4

recommendjgshining | We have carefully selected several similar problems for you:1686 3336 3746 3068 2203Main topic:
gives a string, starting with the second character, that lets you determine whether the preceding string has a periodicity, and then outputs the position and maximum number of periods. (Period to be greater than one)Ideas:first constructs the next[] array, the subscript is I, defines a variable j = i-next[i] is the next array subscript and subscript corresponding value difference, if this difference can divide the subscript I, namely i%j==0, then the subscript i before the string (periodic string length i) must be a The prefix is expressed periodically, the length of the prefix is the difference just obtained, that is, J, then the number of occurrences of this prefix is i/j. So the final output I and i/j can be. Code
#include <cstdio>#include<iostream>#include<cstring>#include<memory>using namespacestd;Charwenben[1000005];intnext1[1000005];voidGETNEXT1 (Char* S,int* Next1,intm) {next1[0]=0; next1[1]=0;  for(intI=1; i<m;i++)    {        intj=Next1[i];  while(j&&s[i]!=S[j]) J=Next1[j]; if(s[i]==S[j]) next1[i+1]=j+1; ElseNext1[i+1]=0; }}intMain () {intL; intt=1;  while(~SCANF ("%d",&L)) {if(l==0)             Break; scanf ("%s", Wenben);        Getnext1 (wenben,next1,l);  for(intI=0; i<l;i++) printf ("i=%d->%d", I,next1[i]); printf ("\ n"); printf ("Test Case #%d\n", t++);  for(intI=2; i<=l;i++)        {            intk=i-(Next1[i]); if(k!=i&& (i)%k==0) printf ("%d%d\n", i,i/k); } printf ("\ n"); }    return 0;}

HDU 1358 Period The prefix length and number of occurrences (the use of the next array of KMP)

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.