Power strings (poj 2406 KMP)

Source: Internet
Author: User
Tags printable characters
Language:DefaultPower strings
Time Limit: 3000 Ms   Memory limit: 65536 K
Total submissions: 33205   Accepted: 13804

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 length of a string s cannot exceed 10 ^ 6. Calculate the maximum n so that S is connected by N identical strings a, for example: "ababab" is composed of N = 3 "AB" connections. "aaaa" is composed of N = 4 "A" connections, "ABCD" is connected by n = 1 "ABCD.

Theorem: if the length of S is Len, S has a circular substring. if and only, Len can be divisible by Len-next [Len, the shortest loop substring is s [Len-next [Len]

Idea: Use the KMP algorithm to obtain the next feature vector of a string. If Len can be divisible by Len-next [Len, the maximum number of cycles N is Len/(LEN-next [Len]); otherwise, it is 1.

Code:

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <string> 7 #include <map> 8 #include <stack> 9 #include <vector>10 #include <set>11 #include <queue>12 #pragma comment (linker,"/STACK:102400000,102400000")13 #define maxn 100514 #define MAXN 200515 #define mod 100000000916 #define INF 0x3f3f3f3f17 #define pi acos(-1.0)18 #define eps 1e-619 typedef long long ll;20 using namespace std;21 22 int N;23 int nextval[1000010];24 char str[1000010];25 26 int get_nextval()27 {28     int i=0;29     int len=strlen(str);30     int j=-1;31     nextval[i]=-1;32     while (i<len)33     {34         if (j==-1||str[i]==str[j])35         {36             i++;37             j++;38             nextval[i]=j;39         }40         else41             j=nextval[j];42     }43     if ((len)%(len-nextval[len])==0)44         return len/(len-nextval[len]);45     else46         return 1;47 }48 49 int main()50 {51     while (scanf("%s",str))52     {53         if (str[0]==‘.‘)54             return 0;55         printf("%d\n",get_nextval());56     }57     return 0;58 }
View code

 

Power strings (poj 2406 KMP)

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.