E-period poj1611 (KMP compute prefix loop section)

Source: Internet
Author: User


E-period
Time limit:1000ms Memory limit:32768kb 64bit IO format:%i64d &%i64u
Submit Status

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 a K, which is a concatenated k times, for some string A. Of course, we also want to know the period K.


Input
The input file consists of several test cases. Each test case consists of 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, has 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

3
Aaa
12
Aabaabaabaab

0


T=i-next[i]; Indicates the loop section of the prefix that has the end of I, about the nature of the next array of KMP, and the previous article has introduced

#include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <cstring    >//#include <bits/stdc++.h>using namespace Std;template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1000001;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#defineM ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) typedef long Long Ll;typedef double db;typedef pair<i nt,int> P; #define BUG printf ("---\ n"); #define MOD 1000000007int Nex[maxn];char a[maxn];void getnext (char*s) {int i=    0,j=-1;    Nex[0]=-1;    int Len=strlen (s); while (I<len) {if (j==-1| |        S[I]==S[J]) nex[++i]=++j;    else J=nex[j];    }}int Main () {//#ifndef Online_judge//freopen ("In.txt", "R", stdin);    Freopen ("Zccccc.txt", "w", stdout);    #endif//Online_judge int n,m,i,j,t,k;    int T;    int Cas=1;        while (read (n) &&n) {printf ("Test case #%d\n", cas++);        scanf ("%s", a);        int Len=strlen (a);        GetNext (a);            for (i=0;i<=len;i++) {t=i-nex[i];            if (i%t==0&&i/t>1) {printf ("%d%d\n", i,i/t);    }} printf ("\ n"); } return 0;}


E-period poj1611 (KMP compute prefix loop section)

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.