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;
    }
  • 相关阅读:
    Qt QApplication 类简介--Qt 类简介专题(四)
    回调函数
    C++类型转换总结
    Debug Error
    C++回调函数(callback)的使用
    Nokia5230连接电脑无线上网
    photoshop cs6\cs5找不到扫描仪的解决办法(Twain_32.8BA补丁下载)
    UML类图几种关系的总结
    实现单点登录
    poj 1151Atlantis线段树求矩形面积并解题报告
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5799689.html
Copyright © 2011-2022 走看看