(KMP 1.5) hdu 1358 Period (use the next array to obtain the minimum cyclic section-the number of cyclic sections for the I character), hdu1358

Source: Internet
Author: User

(KMP 1.5) hdu 1358 Period (use the next array to obtain the minimum cyclic section-the number of cyclic sections for the I character), hdu1358

Question:

Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 3813 Accepted Submission (s): 1862


Problem DescriptionFor each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, aggressive), we want to know whether the prefix is a periodic string. that is, for each I (2 <= I <= N) we want to know the largest K> 1 (if there is one) such that the prefix of S with length I can be written as AK, that is A concatenated K times, for some string. of course, we also want to know the period K.
 
InputThe input file consists of several test cases. each test case consists of two lines. the first one contains N (2 <= N <= 1 000 000)-the size of the string S. the second line contains the string S. the input file ends with a line, having the number zero on it.
 
OutputFor each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length I that has a period K> 1, output the prefix size I and the period K separated by a single space; the prefix sizes must be in increasing order. print a blank line after each test case.
 
Sample Input
3aaa12aabaabaabaab0
 
Sample Output
Test case #12 23 3Test case #22 26 29 312 4
 
RecommendJGShining | We have carefully selected several similar problems for you: 3336 3068 2203 1277


Question Analysis:

KMP. Simple question. This question is actually the same as that of KMP 1.4.



The Code is as follows:

/** Hdu1358.cpp ** Created on: July 15, April 18, 2015 * Author: Administrator */# include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace std; const int maxn = 1000001; int m; // The length of the target string char pattern [maxn]; // The pattern string int nnext [maxn]; // next array. start next directly and the name may be the same as the name specified in the system. /* calculate the next array for the time of O (m) */void get_next () {m = strlen (pattern); nnext [0] = nnext [1] = 0; for (int I = 1; I <m; I ++) {int j = nnext [I]; while (j & Pattern [I]! = Pattern [j]) j = nnext [j]; nnext [I + 1] = pattern [I] = pattern [j]? J + 1: 0 ;}} int main () {int cnt = 1; while (scanf ("% d", & m )! = EOF, m) {scanf ("% s", pattern); get_next (); printf ("Test case # % d \ n", cnt ++ ); /*** traverse the next array. * Number of output cyclic nodes> = 2 */int I; for (I = 0; I <= m; ++ I) {if (nnext [I] = 0) {continue;} int len = I-nnext [I]; if (I % len = 0) {printf ("% d \ n", I, i/len) ;}} printf ("\ n ");}}






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.