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
    ");
        }
    }
  • 相关阅读:
    mysq foreign外键记录
    查询时隐藏部分身份证号
    SpringBoot接收前端参数
    RabbbitMQ安装
    @configurationProperties注解时 idea弹出 Spring Boot Annotion processor not found in classpath
    rpm,yum和apt使用详解
    python人脸识别
    Fuchsia文章汇总
    Androi O Automotive 介绍
    Linux 版本控制工具之rabbitvcs
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6674901.html
Copyright © 2011-2022 走看看