Poj 1961 Period

Source: Internet
Author: User

Link: poj 1961

Question:Returns the maximum number of times K (k> 1) of the substring that starts with the first character of the string and ends with the nth character. If yes, outputs I and K.

For example, aabaabaab has a length of 12.

To 2nd characters, a appears twice. to 6th characters, AAB appears twice,

Up to 9th characters, AAB appears 3 times, and up to 12th characters, AAB appears 4 times

Analysis: this is to find the maximum number of cycles for the substring prefix (including the entire string) of the given string. Just follow the next array.

If (N % (n-next [N]) = 0)

The minimum cycle length is L = N-next [N];

The maximum number of cycles is S = N/L = N/(n-next [N]).


# Include <stdio. h> # include <string. h> int next [1001010] = {-1}; char s [1001010]; void getnext (int n) {int I = 0, j =-1; while (I <n) {If (j =-1 | s [I] = s [J]) {I ++; j ++; next [I] = J;} else J = next [J];} int main () {int N, I, J, K = 0, M; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; scanf ("% s", S); k ++; printf ("Test Case # % d \ n ", k); getnext (n); for (I = 1; I <n; I ++) {J = I + 1; if (J % (J-next [J]) = 0) {M = J/(J-next [J]); If (M> 1) printf ("% d \ n", J, m) ;}} printf ("\ n");} return 0 ;}


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.