POJ2406 Power Strings

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   ar   strong   

傳送門

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
     

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 should print the largest n such that s = a^n for some string a.

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

 

第一道kmp題。。題目意思是求一個字串的迴圈節,求出迴圈節長度之後檢驗即可

 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.