Uvalive-3026 Period (KMP to find the smallest loop section of a string + maximum repetitions)

Source: Internet
Author: User

Reference: http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html

To summarize, if for I in the next array, I% (i-next[i]) = = 0 && next[i]! = 0, then the string loop is described, and

Circulation section length: I-next[i]

Number of cycles: I/(I-next[i])

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - #pragmaComment (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) -  + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) -  - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineE (x) (1 << (x)) to #defineIabs (x) (x) < 0? -(x): (x) + #defineOut (x) printf ("%i64d\n", X) - #defineLowbit (x) (x) & (-X) the #defineRead () freopen ("A.txt", "R", stdin) * #defineWrite () freopen ("B.txt", "w", stdout); $ #defineMAXN 1000000000Panax Notoginseng #defineN 2510 - #defineMoD 1000000000 the using namespacestd; +  A Chara[1000010]; the intp[1000010]; + voidNextintl) - { $     intj=0; $p[1]=0; -      for(intI=2; i<=l;i++) -     { the          while(j>0&& (a[j+1]!=a[i]) j=P[j]; -         if(a[j+1]==a[i]) j+=1;Wuyip[i]=J; the     } - } Wu intMain () - { About     //freopen ("A.txt", "R", stdin); $     intn,j=1; -      while(~SCANF ("%d",&N)) -     { -         if(n==0) Break; Aprintf"Test Case #%d\n", J + +); +scanf"%s", A +1); the         intL=strlen (A +1); - next (l); $          for(intI=1; i<=l;i++) the         { the             if(i% (i-p[i]) = =0&&p[i]!=0) the             { theprintf"%d%d\n", i,i/(i-p[i])); -             } in         } theprintf"\ n"); the     } About     return 0; the}

Uvalive-3026 Period (KMP to find the smallest loop section of a string + maximum repetitions)

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.