POJ 2406 Power Strings 简单KMP

Power Strings

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33389   Accepted: 13869

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.

题意:求一个字符串有多少个循环节,如果没有出现循环就输出1

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 1000009
using namespace std;

char a[N];
int n;
int next[N];
//记住一个性质,1到nxet[n]这一串字符和n-next[n]+1到n这一段字符是相匹配的
void getnext()
{
    int i,j;
    i=0;
    j=-1;
    next[0]=-1;

    while(i<n)
    {
        if(j==-1||a[i]==a[j])
        {
            i++;
            j++;
            next[i]=j;
        }
        else
        j=next[j];
    }
}

int main()
{
    while(~scanf("%s",a))
    {
        if(a[0]=='.') break;

        n=strlen(a);
        getnext();

        if(n%(n-next[n])==0)
        cout<<n/(n-next[n])<<endl;
        else
        cout<<1<<endl;

//        for(int i=0;i<=len;i++)
//        cout<<next[i]<<" ";
//        cout<<endl;

//        if(next[len]==0)
//        cout<<1<<endl;
//        else
//        printf("%d\n",next[len]-1);

    }
    return 0;
}
时间: 2024-12-07 18:26:55

POJ 2406 Power Strings 简单KMP的相关文章

poj 2406 Power Strings(KMP求循环次数)

题目链接:http://poj.org/problem?id=2406 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, e

poj 2406 Power Strings(kmp循环节)

题目链接:http://poj.org/problem?id=2406 题目大意:如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]. 例如:      a    b    a    b    a    b next:-1   0    0    1    2    3    4 next[n]==4,代表着,前缀abab与后缀abab相等的最长长度,这说明,ab这两个字母为一个循环节,长度=n-next[n]; 1 #include <iostream> 2

poj 2406 Power Strings【KMP】

点击打开题目 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33548   Accepted: 13935 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】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37564   Accepted: 15532 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)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 34601   Accepted: 14319 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的应用】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33595   Accepted: 13956 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)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33335   Accepted: 13852 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def"

poj 2406 Power Strings(kmp求一个串的重复子串)

题意:重复子串次数 思路:kmp #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define MaxSize 1000005 int next[MaxSize]; void GetNext(char t[]){//求next数组 int j,k,len; j=0; k=-1; next[0]=-1; len=strlen(t); while(j<len){

POJ 2406 Power Strings KMP运用题解

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