(KMP 1.5) HDU 1358 Period (use next array to find the smallest loop-the number of cyclic sections for the I-character)

Source: Internet
Author: User

Topic:

PeriodTime 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 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 Input
3aaa12aabaabaabaab0

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

recommendjgshining | We have carefully selected several similar problems for you:3336 3068 2203 1277 1867


Topic Analysis:

Kmp. Simple question. This question is in fact the same idea as the KMP 1.4.



The code is as follows:

/* * hdu1358.cpp * *  Created on:2015 April 18 *      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];//pattern string int nnext[maxn];//next array. Direct from next may be the same as the predetermined name in the system./*o (m) time for next array */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 ();p rintf ("Test case #%d\n", cnt+ +);/** * Iterates over the next array. * Output Cyclic section number >=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%d\n", I,i/len);}} printf ("\ n");}}






(KMP 1.5) HDU 1358 Period (use next array to find the smallest loop-the number of cyclic sections for the I-character)

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.