POJ 2406 Power Strings

Source: Internet
Author: User
Tags printable characters

Power Strings
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 36926 Accepted: 15254

Description

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. 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.

Output

For each s should print the largest n such, that s = a^n for some string a.

Sample Input

Abcdaaaaababab.

Sample Output

143MP algorithm:
#include <stdio.h> #include <string.h> #define MAX 1100000char str[max];int f[max];void getfail ()//Mismatch function The array f defined by this function indicates that when the original string is mismatch with the target string {          //can be based on f[] to jump to the corresponding position of the original string to improve the efficiency of the operation int i,j;int len=strlen (str); J=0;f[0]=f[1]=0;for (I=1 ; i<len;i++) {J=f[i];while (J && str[i]! = str[j]) j = f[j];f[i+1] = str[i] = = str[j]?j+1:0;}} int main () {int n,m,j,i,s,t;while (scanf ("%s", str) &&str[0]!= '. ') {getfail (); int len = strlen (str), if (len% (Len-f[len]))//here Len-f[len] means the length of the maximum loop section in the original string printf ("1\n");  len% (Len-f[len] Surplus number proves that the number of nodes removed from the maximum loop section still has                 //non-cyclic characters else             printf ("%d\n", len/(Len-f[len]));// If the maximum loop section of the original character can represent the entire string                                     , the output cycle will  return 0;}

POJ 2406 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.