UVa 1328 (KMP for string cycles) Period

Source: Internet
Author: User

At the time of learning KMP also did this problem, now it seems Rujia code to streamline some, after all, the shorter the better remember, the less prone to error.

and the code style of the recursive mismatch function of KMP and the code style of the Aho-corasick auto-mismatch function in the back are also consistent.

1#include <cstdio>2 3 Const intMAXN =1000000+Ten;4 CharS[MAXN];5 intF[MAXN];//a mismatch function6 7 intMain ()8 {9     //freopen ("In.txt", "R", stdin);Ten  One     intN, Kase =0; A      while(SCANF ("%d", &n) = =1&&N) -     { - GetChar (); the          for(inti =0; I < n; i++) S[i] =GetChar (); -  -         //recursive solution of mismatch function -f[0] = f[1] =0; +          for(inti =1; I < n; i++) -         { +             intj =F[i]; A              while(j && s[i]! = s[j]) j =F[j]; atf[i+1] = s[i] = = S[j]? j+1:0; -         } -  -printf"Test Case #%d\n", ++Kase); -          for(inti =2; I <= N; i++) -             if(F[i] && i% (i-f[i]) = =0) inprintf"%d%d\n", I, I/(I-f[i])); -printf"\ n"); to     } +  -     return 0; the}
code June

UVa 1328 (KMP for string cycles) Period

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.