Zoj1905power Strings (kmp| | Suffix array +rmq to find the Loop section)

Source: Internet
Author: User
Tags printable characters

Given Strings A and b we define a*b to be their concatenation. For example, if a = "abc" and B = "Def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer are defined in the normal way:a^0 = "" (The empty string) and a^ (n+1) = A * (a^n).

Input

Each test case was a line of input representing S, a string of printable characters.


<b< dd= "" >

Output

For each s should print the largest n such, that s = a^n for some string a. The length of S'll be is at least 1 and would not exceed 1 million characters. A line containing a period follows the last test case.

Sample Input

Abcd
Aaaa
Ababab
.

Sample Output

1
4
3

The maximum loop length is calculated.

KMP can beg, have done before, see.

Http://www.cnblogs.com/hua-dong/p/8016873.html

Http://www.cnblogs.com/hua-dong/p/8016916.html

The suffix array is implemented here (although it seems to be stuck, it can only be implemented with KMP).

#include <cmath>#include<cstdio>#include<string>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>intMinintXintY) {if(x<y)returnXreturny;}using namespacestd;Const intmaxn=1000010;CharCH[MAXN];structsa{intRANK[MAXN],SA[MAXN],TSA[MAXN],A[MAXN],CNTA[MAXN],B[MAXN],CNTB[MAXN]; intht[maxn],min[maxn][ -],n; voidGet_sa () {N=strlen (ch+1);  for(intI=0; i<= -; i++) cnta[i]=0;  for(intI=1; i<=n;i++) cnta[ch[i]]++;  for(intI=1; i<= -; i++) cnta[i]+=cnta[i-1];  for(inti=n;i>=1; i--) sa[cnta[ch[i]]--]=i; rank[sa[1]]=1;  for(intI=2; i<=n;i++) rank[sa[i]]=rank[sa[i-1]]+ (ch[sa[i]]==ch[sa[i-1]]?0:1);  for(intL=1; rank[sa[n]]<n;l<<=1){             for(intI=1; i<=n;i++) cnta[i]=cntb[i]=0;  for(intI=1; i<=n;i++) cnta[a[i]=rank[i]]++;  for(intI=1; i<=n;i++) cntb[b[i]=i+l<=n? RANK[I+L]:0]++;  for(intI=1; i<=n;i++) cnta[i]+=cnta[i-1],cntb[i]+=cntb[i-1];  for(inti=n;i>=1; i--) tsa[cntb[b[i]]--]=i;  for(inti=n;i>=1; i--) sa[cnta[a[tsa[i]]]--]=Tsa[i]; rank[sa[1]]=1;  for(intI=2; i<=n;i++) rank[sa[i]]=rank[sa[i-1]]+ (a[sa[i]]==a[sa[i-1]]&&b[sa[i]]==b[sa[i-1]]?0:1); }    }    voidget_hgt () { for(intI=1, j=0; i<=n;i++){            if(j) j--;  while(ch[i+j]==ch[sa[rank[i]-1]+J]) J + +; Ht[rank[i]]=J; }    }    voidget_rmq () { for(intI=1; i<=n;i++) min[i][0]=Ht[i];  for(intI=1;(1<<i) <=n;i++)          for(intj=1; j+ (1<<i)-1<=n;j++) {Min[j][i]=min (min[j][i-1],min[j+ (1<< (I-1))][i-1]); }    }    intQUERY_RMQ (intLintR) {if(l>r) swap (L,R); l++; intK=LOG2 (r-l+1); returnMin (min[l][k],min[r-(1&LT;&LT;K) +1][k]); }    voidsolve () {intans=1;  for(intI=1; i<=n;i++){            if(n%i!=0)Continue; if(I+QUERY_RMQ (rank[1],rank[1+i]) = =N) {ans=n/i; Break; }} printf ("%d\n", ans); }}sa;intMain () { while(~SCANF ("%s", ch+1)){        if(ch[1]=='.')return 0;        Sa.get_sa ();        SA.GET_HGT ();        SA.GET_RMQ ();     Sa.solve (); }  return 0;}

Zoj1905power Strings (kmp| | Suffix array +rmq to find the 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.