Poj2406 power strings

Source: Internet
Author: User

Portal

Power strings
Time limit:3000 Ms   Memory limit:65536 K
     

Description

Given two 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 is defined in the normal way: a ^ 0 = "" (the empty string) and a ^ (n + 1) = A * (a ^ N ).

Input

Each test case is a line of input representing S, a string of printable characters. the length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you shoshould print the largest N such that S = a ^ N for some string.

Sample Input

abcdaaaaababab.

Sample output

143

Hint

This problem has huge input, use scanf instead of CIN to avoid time limit exceed.

Source

Waterloo local 2002.07.01

 

The first KMP question .. The question is to find the loop section of a string, and then check the length of the loop section.

 1 #include<set> 2 #include<queue> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<iostream> 7 #include<algorithm> 8 using namespace std; 9 const int N = 1000010;10 #define For(i,n) for(int i=1;i<=n;i++)11 #define Rep(i,l,r) for(int i=l;i<=r;i++)12 char s[N];13 int next[N],n;14 15 void BuildNext(char s[]){16     next[0]=next[1]=0;17     For(i,n-1){18         int j=next[i];19         while(j&&s[i]!=s[j]) j=next[j];20         if(s[i]==s[j])  next[i+1]=j+1;21         else            next[i+1]=0;22     }23 }24 25 int main(){26     while(scanf("%s",&s),s[0]!=‘.‘){27         n=strlen(s);BuildNext(s);28         int rpt = n-next[n];29         int i = n;30         while(i&&i-next[i]==rpt) i=next[i];31         if(i) printf("1\n");32         else  printf("%d\n",n/rpt);33     }34     return 0;35 }
Codes

 

Poj2406 power strings

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.