zoukankan      html  css  js  c++  java
  • poj-2406 Power Strings

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 65854   Accepted: 27197

    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.
     
     
    思路:next[i]表示以第i个字符结束的最大后缀和最大前缀相等。n-next[n]则为最小的循环节。若n%(n-next[n])==0,那么n/(n-next[n])则为循环节出现的最大次数。
     
    #include <iostream>
    #include<string.h>
    using namespace std;
    const int maxn = 1e6+10;
    char a[maxn];
    int ne[maxn];
    int main()
    {
        while(scanf("%s",a+1)&&a[1]!='.')
        {
            memset(ne,0,sizeof(ne));
            int len = strlen(a+1);
            for(int i=2,j=0;i<=len;i++)
            {
                while(j&&a[i]!=a[j+1]) j=ne[j];
                if(a[i]==a[j+1]) j++;
                ne[i]=j;
            }
            if(len%(len-ne[len])==0) printf("%d
    ",len/(len-ne[len]));
            else
                printf("1
    ");
        }
        return 0;
    }
  • 相关阅读:
    Jsuop Whitelist
    Conductor
    nats
    jersey
    Metrics
    OpenResty api 网关
    DHCP、DHCP Snooping及DHCP relay工作原理入门及实践(转)
    使用派生镜像(qcow2)
    websockify文档
    noVNC使用——访问多台vnc
  • 原文地址:https://www.cnblogs.com/wjc2021/p/11300065.html
Copyright © 2011-2022 走看看