Algorithm Problem POJ 1961 Period (KMP, shortest cycle section)

Source: Internet
Author: User
Tags cas printf

Topic Link:

poj:http://poj.org/problem?id=1961

hdu:http://acm.hdu.edu.cn/showproblem.php?pid=1358

zoj:http://acm.zju.edu.cn/onlinejudge/showproblem.do?problemcode=2177

The main effect of the topic:

Given a string s of length n, the shortest link for each prefix is obtained. In other words, for each i (2<=i<=n), a maximum integer k>1 (if k exists) is obtained, which makes the prefix of the first I character of S a string repeated K-times. Outputs all the I and corresponding k that exist K.

For example, for string Aabaabaabaab, only K exists when i=2,6,9,12, and is 2,2,3,4

Analysis and Summary:

Very classic A topic, each big OJ have included.

When I started this problem, just read KMP, did not see the shortest circulation section of the relevant information, their own mess, took two discrete math class to think, the result is finally AC = =, but unfortunately, the results found just again HDU and Zoj over, and Poj or wa ...

Then learn the "Introduction Classic algorithm-Training guide" above the method, on the POJ to successfully AC.

Although did not succeed in the Poj AC, but after this question ponder, to kmp the mismatch function obtains next the process and the principle to have the deeper understanding and the realization.

Code:

1. The code of his own disorderly, Poj can't ac, beg break

 #include <cstdio> #include <cstring> const int MAXN = 1000005;  
Char T[MAXN];  
int F[MAXN];  
    
int n;  
    void Getfail (char *p, int *f) {f[0]=f[1]=0;  
    int start=1;  
    int N=strlen (P);   
    
    BOOL Flag=true;  
        for (int i=1; i<n; ++i) {int j=f[i];  
            while (J && P[i]!=p[j]) {flag=false;  
        J=F[J];  
            } if (P[i]==p[j]) {f[i+1]=j+1;  
                if (f[i+1]==1) {flag=true;  
            start=i; } if (flag && (i+1) >=start*2 && (i+1)%start==0) {printf ("%d%d\n", i+1, (i  
            +1)/start);  
            }} else{flag=true;  
            start=i+1;  
        f[i+1]=0;  
    int main () {int cas=1;  
    Char ch;  
        while (~SCANF ("%d%*c", &n) && N) {printf ("Test case #%d\n", cas++); Gets (T);  
        Getfail (T,F);  
    Puts ("");  
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.