zoukankan      html  css  js  c++  java
  • G

    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.
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstring>
    using namespace std;
    #define MAXN 1000001
    typedef long long LL;
    /*
    给定一个串,找最短循环节
    */
    char s[MAXN];
    int Next[MAXN];
    void kmp_pre(int m)
    {
        int j,k;
        j = 0;k = Next[0] = -1;
        while(j<m)
        {
            if(k==-1||s[j]==s[k])
                Next[++j] = ++k;
            else
                k = Next[k];
        }
    }
    int main()
    {
        while(scanf("%s",s))
        {
            if(s[0]=='.') break;
            int l = strlen(s);
            kmp_pre(l);
            int ans = l - Next[l];
            if(l%ans==0)
                printf("%d
    ",l/ans);
            else
                printf("1
    ");
        }
    }
  • 相关阅读:
    增删改查
    全局配置文件mappers_sql映射注册
    全局配置文件<typeAliases>别名配置
    接口式编程小结
    Mybatis_接口编程
    Mybatis_HelloWord
    xml中标签含义
    Spring中Bean的基本概念
    ACM 第十四天
    ACM 第十三天
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6674901.html
Copyright © 2011-2022 走看看