zoukankan      html  css  js  c++  java
  • POJ 2406 Power Strings (求字符串循环节出现的次数)

    Power Strings
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 44217   Accepted: 18449

    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
    /*
     * POJ 2406 Power Strings
     * 求字符串循环节出现的次数
     * 
     * 结论:字符串S有最小循环节(p<len)的充要条件是(len-next[len])|len,next[len]>0
     * 其中p是循环节长度,len是字符串S的长度
     */
    
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    
    const int MAXN = 1000000+10;
    
    void Get_Next(char* P,int* next)
    {
        int i=0,k=-1;
        int plen=strlen(P);
        next[0]=-1;
        while(i<plen)
        {
            if(k==-1||P[k]==P[i])
            {
                i++,k++;
                next[i]=k;
            }
            else k=next[k];
        }
    }
    char str[MAXN];
    int nxt[MAXN];
    
    int main()
    {
        while(scanf("%s",str)==1)
        {
            if(str[0]=='.') break;
            Get_Next(str,nxt);
            int n=strlen(str);
            int k=n-nxt[n];
            if(n%k==0&&n/k>1) printf("%d
    ",n/k);
            else printf("1
    ");
        }
        return 0;
    }
  • 相关阅读:
    Mac下使用charles遇到的问题以及解决办法
    webp图片实践之路
    一个粗心的Bug,JSON格式不规范导致AJAX错误
    IE6/7下空div占用空间的问题
    通俗易懂的来讲讲DOM
    Javascript
    简单入门canvas
    HTML5 Boilerplate
    网页字体知识
    备战CKA每日一题——第8天 | initContainer概念、用法、使用场景简介;k8s secret env、volume考题引出
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5799689.html
Copyright © 2011-2022 走看看