poj2406--Power Strings(KMP求最小循环节)

Power Strings

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33178   Accepted: 13792

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

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

Waterloo local 2002.07.01

用next数组求出整个数组的最大前缀,如果整个串是用循环节组成的,那么 n - next[n] 也就是最小循环节,验证最小循环节会被n整出。

#include <cstdio>
#include <cstring>
#include <algorithm>
int next[1100000] ;
char str[1100000] ;
void getnext(int l)
{
    int j = 0 , k = -1 ;
    next[0] = -1 ;
    while(j < l)
    {
        if( k == -1 || str[j] == str[k] )
        {
            j++ ;
            k++ ;
            next[j] = k ;
        }
        else
            k = next[k] ;
    }
}
int main()
{
    int l , m ;
    while(scanf("%s", str)!=EOF)
    {
        if( str[0] == '.' ) break;
        l = strlen(str);
        getnext(l) ;
        m = next[l];
        if( l % (l-m) != 0 )
            printf("1\n");
        else
        {
            m = l / ( l-m );
            printf("%d\n", m);
        }
        memset(str,0,sizeof(str));
    }
    return 0;
}
时间: 2024-08-02 11:03:59

poj2406--Power Strings(KMP求最小循环节)的相关文章

KMP + 求最小循环节 --- POJ 2406 Power Strings

Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少次得到. analyse: KMP之next数组的运用.裸的求最小循环节. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Accepted* Submission Date: 20

模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. 例: abca---添加bc,成为abcabc abcd---添加abcd,成为abcdabcd aa---无需添加 analyse: 经典的求最小循环节. 首先给出结论:一个字符串的最小循环节为:len-next[len]. 证明: 举个例子:abcab

KMP + 求最小循环节 --- HDU 1358 Period

Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度的字符串是否是重复串,如果是,输出当前位置,并输出重复串的周期. analyse: 还是next数组的运用,详见上一篇博客. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Acce

UVA - 10298 Power Strings (KMP求字符串循环节)

Description Problem D: Power Strings 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, exponentiati

poj2406 Power Strings (KMP)

这题跟HDU 1358 Period (KMP)差不多,稍微修改代码就行了. 关于KMP的更多知识,请关注从头到尾彻底理解KMP(2014年8月4日版). #include<stdio.h> #include<string.h> int n,next[1000000]; char p[1000000]; void getnext() { int k=0,j=1; next[0]=-1;next[1]=0; while (j<n) { if (k==-1||p[j]==p[k]

poj2406 kmp 求最小循环字串

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 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 = "

(KMP 1.5)hdu 1358 Period(使用next数组来求最小循环节——求到第i个字符的循环节数)

题目: Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3813    Accepted Submission(s): 1862 Problem Description For each prefix of a given string S with N characters (each character has an A

nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】

循环小数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1 描述 我们可爱的 c小加 近段儿正在潜心研究数学,当他学习到循环小数这一部分时不是太明白循环体是什么意思(比如说3.23232323的循环体是23.2323.23232323),假设我们现在的循环小数都是严格循环的并且有限的,也就是说不出现2.16666666(循环体为6,长度为1)的情况,只有123123这样的循环出现.给他一个小数,他需要找出最小循环体的长度.循环体和循环的次数,请你帮他解决这个问题. 输入 输

POJ2406 Power Strings(KMP,后缀数组)

这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即为结果. 若lcp(suffix(0), suffix(k))的长度为n- k,则将串每k位分成一段,则第1段与第2段可匹配,又可推得第2段与第3段可匹配……一直递归下去,可知每k位都是相同的,画图可看出匹配过程类似于蛇形. 用倍增算法超时,用dc3算法2.5秒勉强过. #include<cstdi

POJ2406 Power Strings next数组应用

题目描述: 给定一个字符串,求其最大循环次数(即求最小循环节长度) 输入样例 abcd ababab aaaa . 输出样例 1 3 4 解题思路: KMP算法中next数组的应用. len-next[len]表示的是字符串相同前缀空出来的一段,由next数组性质可知,这一段可以不断向前推出相等,所以只要判断len是否可以整除len-next[len]就可以了.否则就是1. 代码如下 #include <cstdio> #include <cstring> const int ma