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

    Power Strings
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 26444   Accepted: 11072

    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
    题目大意:求字符串最多能够由多少个连续重复子串连接而成
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    char str1[1000010];
    int next[1000010];
    
    void getnext(char str[])
    {
        int k = -1;
        int j = 0;
        next[0] = -1;
        int nLen = strlen(str);
        while(j < nLen)
        {
            if (k == -1 || str[k] == str[j])
            {
                k++;
                j++;
                next[j] = k;
            }
            else
            {
                k = next[k];
            }
        }
    }
    
    int main()
    {
        while(scanf("%s", str1) != EOF)
        {
            if (str1[0] == '.')
            {
                break;
            }
            getnext(str1);
            int nLen = strlen(str1);
            if (nLen % (nLen - next[nLen]) == 0)
            {
                printf("%d\n", nLen / (nLen - next[nLen]));
            }
            else
            {
                printf("1\n");
            }
        }
        return 0;
    }
  • 相关阅读:
    安装PyExecJS出现问题
    常用模块
    python2和python3的区别
    正则表达式
    MySQL之表的关系
    Python与MySQL的交互
    select 查询
    MySQL之表的连接
    bug
    黑盒测试-判定表驱动法
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3114055.html
Copyright © 2011-2022 走看看