• Power Strings


    问题 D: 4.5.17 Power Strings

    时间限制: 3 Sec  内存限制: 64 MB
    提交: 2995  解决: 921
    [提交][状态][讨论版]

    题目描述

    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).
     

    输入

    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.
     

    输出

    For each s you should print the largest n such that s = a^n for some string a.
     

    样例输入

    abcd
    aaaa
    ababab
    .

    样例输出

    1
    4
    3
    思路:
    找到最小的重复序列
    kmp中的next数组就是这个作用
    #include  <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    int pext[1000000] = {-5};
    char s[1000000];
    int len1;
    void get_next(char *T,int *next)
    {
        int k = -1;
        int j = 0;
        pext[j] = k;
        while(j < len1)
        {
            if((k == -1) || (T[j] == T[k]))
            {
                k++;
                j++;
                pext[j] = k;
            }
            else
            {
                k = pext[k];
            }
        }
    }
     
    int main()
    {
        while(1)
        {
            scanf("%s",s);
            if(strcmp(s,".") == 0)
            {
                return 0;
            }
            else
            {
                int sum = 0;
                len1 = strlen(s);
                get_next(s,pext);
                int z; 
                z =  len1 - pext[len1];
                if(len1 % z ==0)
                {
                    for(int i=1;i<=len1;i++)
                    {
                        if(i%z==0)
                            sum++;    
                    }
                    printf("%d
    ",sum); 
                }
                else
                {
                    printf("1
    ");
                }
            }
        }
        return 0;
    } 
     
  • 相关阅读:
    5.7填数字游戏求解
    5.6判断回文数字
    5.5百钱买百鸡问题
    5.4三色球问题
    5.3哥德巴赫猜想的近似证明
    5.2求两个数的最大公约数和最小公倍数
    5.1舍罕王的失算
    4.19递归反向输出字符串
    Elasticsearch 安装
    linux 安装nginx步骤
  • 原文地址:https://www.cnblogs.com/yfr2zaz/p/11082668.html
Copyright © 2020-2023  润新知