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 = "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.

题意:

给你一个字符串,求该字符串最多由多少个循环字串构成。

题解:

  定义一个整型变量 m;   //m表示字符串长度

  用kmp里的next数组 求出字符串的最长前缀长度,然后判断 m 是否能被 m-next[m] 整除,即 m%(m-next[m])是否等于0,如果能被整除,m-next[m]为最短循环字串,否则最短循环字串长度等于整个字符串长度。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int next[1000002];
char a[1000002];

void getNext()
{
    int i=-1,j=0;
    next[0]=-1;
    int cnt=0;
    while(a[j])
    {
        if(a[i]==a[j]||i==-1)
        {
            next[++j]=++i;
        }
        else
            i=next[i];
    }
}
int main()
{
    while(scanf("%s",a)!=EOF)
    {
        if(a[0]==‘.‘) break;
        int m=strlen(a);
        getNext();
        int cc=1;
        if(m%(m-next[m])==0)  //如果条件满足,m-next[m]为最短循环字串,然后求出该字符串由多少个最短循环字串构成
            cc=m/(m-next[m]);
        cout<<cc<<endl;
    }
    return 0;
}

  

时间: 2024-10-12 16:53:54

poj2406 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

hdu 3746 Cyclic Nacklace (KMP求最小循环节)

//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; char a[100010]; int next[100010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[i]==a[j]) i

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 = "

(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这样的循环出现.给他一个小数,他需要找出最小循环体的长度.循环体和循环的次数,请你帮他解决这个问题. 输入 输

poj 2406 求最短重复字串

题解: KMP中next数组的巧妙运用.在这里我们假设这个字符串的长度是len,那么如果len可以被len-next[len]整除的话,我们就可以说len-next[len]就是那个最短子串的长度为什么呢? 假设我们有一个字符串ababab,那么next[6]=4对吧,由于next的性质是,匹配失败后,下一个能继续进行匹配的位置,也就是说,把字符串的前四个字母,abab,平移2个单位,这个abab一定与原串的abab重合(否则就不满足失败函数的性质),这说明了什么呢,由于字符串进行了整体平移,而

KMP模版 &amp;&amp; KMP求子串在主串出现的次数模版

int next[MAX_LEN]; void creat_next(char *S, int Sn) { int k, q; next[0] = 0; for(k=0, q=1; q<Sn; q++){ while(k>0 && S[k]!=S[q]) k = next[k-1]; if(S[k] == S[q]){ k++; } next[q] = k; } } bool Judge(char *S, char *T, int Sn, int Tn)//S是子串,T是主串