zoukankan      html  css  js  c++  java
  • POJ 2406 Power Strings

    Power Strings

     

    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

    abcd
    aaaa
    ababab
    .
    

    Sample Output

    1
    4
    3
    

    Hint

    This problem has huge input, use scanf instead of cin to avoid time limit exceed.
     
     
     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 const int maxn=1000005;
     6 
     7 int next[maxn];
     8 
     9 void getnext(char str[])
    10 {
    11     int i,j;
    12     int len=strlen(str);
    13     i=0;j=-1;
    14     next[i]=j;
    15     while(i<=len)
    16     {
    17         if(j==-1||str[i]==str[j])
    18         {
    19             i++;
    20             j++;
    21             next[i]=j;
    22         }
    23         else
    24         j=next[j];
    25     }
    26 }
    27 
    28 int main()
    29 {
    30     //freopen("in.txt","r",stdin);
    31     int cnt,i;
    32     char s[maxn];
    33     while(scanf("%s",s)&&strcmp(s,".")!=0)
    34     {
    35         getnext(s);
    36         int len=strlen(s);
    37         if(len%(len-next[len])==0)
    38         printf("%d
    ",len/(len-next[len]));
    39         else
    40         printf("%d
    ",1);
    41     }
    42     return 0;
    43 }
     
  • 相关阅读:
    字符串处理类
    PageHelper
    JavaScriptPlus操作类
    Http 数据操作
    解压 压缩 C#
    验证码生成 C#
    MySecurity(安全类)
    博客搬迁至wordpress站点
    我的前端MVC之路
    三个css3趣玩小试
  • 原文地址:https://www.cnblogs.com/homura/p/4711084.html
Copyright © 2011-2022 走看看