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

    http://poj.org/problem?id=2406

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 50627   Accepted: 21118

    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.

    Source

     
    求最短循环节出现次数
     1 #include <algorithm>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int N(2333333);
     8 int ans,len,p[N];
     9 char s[N];
    10 
    11 inline void Get_next()
    12 {
    13     for(int i=2,j=0;i<=len;i++)
    14     {
    15         for(;s[i]!=s[j+1]&&j>0;) j=p[j];
    16         if(s[i]==s[j+1]) j++;
    17         p[i]=j;
    18     }
    19 }
    20 
    21 int main()
    22 {
    23     for(scanf("%s",s+1);s[1]!='.';scanf("%s",s+1))
    24     {
    25         memset(p,0,sizeof(p));
    26         len=strlen(s+1);
    27         Get_next();
    28         int tmp=len-p[len];
    29         if(len%tmp) puts("1");
    30         else printf("%d
    ",len/tmp);
    31     }
    32     return 0;
    33 }
    ——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
  • 相关阅读:
    Spring 中各种通知
    Spring 中的注解
    Spring_DI利用set方法赋值Demo
    Beta冲刺总结
    用户使用调查报告
    Beta(7/7)
    Beta(6/7)
    Beta(5/7)
    Beta(4/7)
    Beta(3/7)
  • 原文地址:https://www.cnblogs.com/Shy-key/p/7382656.html
Copyright © 2011-2022 走看看