poj1961--Power Strings(kmp:求循环串的次数)

Power Strings

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33163   Accepted: 13784

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.0

#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( m == -1  )
            printf("1\n");
        else if(m == l)
            printf("%d\n", l);
        else 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-10-09 23:57:34

poj1961--Power Strings(kmp:求循环串的次数)的相关文章

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

UVA 10298 - Power Strings(KMP)

UVA 10298 - Power Strings 题目链接 题意:本意其实就是,给定一个字符串,求出最小循环节需要几次循环出原字符串 思路:利用KMP中next数组的性质,n - next[n]就是最小循环节,然后n / 循环节就是答案 代码: #include <cstdio> #include <cstring> const int N = 1000005; char str[N]; int next[N]; void getnext() { int n = strlen(s

poj 2406 Power Strings(KMP&amp;思维)

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

poj 2406 Power Strings KMP匹配

对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],假设t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比如字符串"abababab", a  b a b a b a b * next     -1 0 1 2 3 4 5 6  7 考虑这样的模式匹配,将"abababab#"当做主串,"abababab*"当做模式串,于是进行匹配到n(n=8)时,出现了不匹配: 主串   

POJ 2406 Power Strings KMP运用题解

本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都觉得是可以利用next数组的,但是自己想了很久没能这么简洁地总结出来,也只能查查他人代码才恍然大悟,原来可以这么简单地区求一个周期字符串的最小周期的. 有某些大牛建议说不应该参考代码或者解题报告,但是这些大牛却没有给出更加有效的学习方法,比如不懂KMP,难倒不应该去看?要自己想出KMP来吗?我看不太可能有哪位大牛可以直接自己"重新创造出KMP"来吧. 好吧,不说"创造KMP

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]

UVA10298 Power Strings [KMP]

题目传送门 Power Strings 格式难调,题面就不放了. 一句话题意,求给定的若干字符串的最短循环节循环次数. 输入样例#1: abcd aaaa ababab . 输出样例#1: 1 4 3 就这样. 分析: 一道思路神奇的题目,需要深入理解$KMP$的$next$数组. 如果自己写几个字符串推一下就可以发现,一个由循环节构成的字符串,从第二个循环节开始$next$值是依次递增的,因为$next$数组的本质是表示$0\~i-1$的最长公共前缀后缀长度.也就不难想到,只要判断一下$nex

hdu 2406 Power Strings KMP

Power Strings                                                               Time Limit:3000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Description Given two strings a and b we define a*b to be their concatenation. For example, if a =

poj 2406 Power Strings(kmp的nxt数组找最小循环节)

题目链接:poj 2406 Power Strings 题意: 给你一个字符串,让你找出这个字符串的最大循环次数,及最小循环节. 题解: 用kmp的nxt数组搞搞,L=j-nxt[j],为前缀j的最小循环节. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define F(i,a,b) for(int i=(a);i<=(b);++i) 5 using namespace std;