miércoles, 6 de marzo de 2013

UVA 10298 Power Strings

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).
Each test case is a line of input representing s, a string of printable characters. For each s you should print the largest n such that s = a^n for some string a. 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.

Sample Input

abcd
aaaa
ababab
.

Output for Sample Input

1
4
3


1 comentario:

  1. #include<stdio.h>
    #include<string.h>
    long a[1000005],i,j,n,l;
    char cd[1000002];

    int main(){
    a[0]=-1;
    while(gets(cd)){
    if(cd[0]=='.')
    break;

    l=strlen(cd);
    j=0;

    for(i=1;i<l;i++){

    while(j!=-1){
    if(cd[i]==cd[j]){
    j++;
    break;
    }else{
    j=a[j];
    }

    }

    if(j==-1)
    j++;
    a[i+1]=j;
    }

    n=l-a[l];
    if(n>0){
    n=l/n;
    printf("%ld\n",n);
    }else if (l==2&&cd[0]==cd[1]){
    printf("2\n");
    }else{
    printf("1\n");
    }

    }
    }

    ResponderEliminar