zoukankan      html  css  js  c++  java
  • 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;
    } 
     
  • 相关阅读:
    江の島西浦写真館2-1
    江の島西浦写真館1-2
    Oracle 查询表空间使用情况
    Oracle 的开窗函数 rank,dense_rank,row_number
    oracle11G 用户密码180天修改概要文件过程
    CentOS6 安装 MySQL5.7
    linux下SS 网络命令详解
    CentOS6 网络设置
    redhat 6 红帽6 Linux 网络配置
    Oracle分析函数——函数列表
  • 原文地址:https://www.cnblogs.com/yfr2zaz/p/11082668.html
Copyright © 2011-2022 走看看