POJ2406Power Strings[KMP 失配函数]

Power Strings

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 45005   Accepted: 18792

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


只需要求n的错位部分n-f[n]是不是循环节

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+5;
int n,f[N];
char p[N];
int main(){
    int cas=0;
    while(true){
        scanf("%s",p);
        n=strlen(p);
        if(p[0]==‘.‘) break;
        f[0]=f[1]=0;
        for(int i=1;i<n;i++){
            int j=f[i];
            while(j&&p[j]!=p[i]) j=f[j];
            f[i+1]=p[j]==p[i]?j+1:0;
        }
        if(n%(n-f[n])==0) printf("%d\n",n/(n-f[n]));
        else printf("1\n");
    }
}
时间: 2024-10-28 23:17:30

POJ2406Power Strings[KMP 失配函数]的相关文章

poj2406 Power Strings(kmp失配函数)

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

POJ--2406Power Strings+KMP求字符串最小周期

题目链接:点击进入 事实上就是KMP算法next数组的简单应用.假设我们设这个字符串的最小周期为x 长度为len,那么由next数组的意义,我们知道len-next[len]的值就会等于x.这就是这个题目的关键点. 代码例如以下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=1000000+100; char str[maxn]; in

POJ 2752 Seek the Name, Seek the Fame kmp失配函数next应用

点击打开链接 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12791   Accepted: 6304 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give

POJ2406-Power Strings(kmp循环节)

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

POJ1961[KMP 失配函数]

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 16776   Accepted: 8077 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the

poj2406--Power Strings+KMP求周期

先把结论摆出来:对于长为j的字符串str[1..j],next[j]=k,则令d=j-k;如果j%d==0,则这个字符串是一个 周期串,前d个字符是其最小的循环结,共包含j/d个循环节. 现在来解决两个问题: 1)前d个字符是其循环结 下标  1   2  3  4  5  6  7  8 字符串 a   b  a  b  a  b  a  b next值 0   0  1  2  3  4  5  6 next[j]=k表示str[1...k]与str[j-k+1..j]是完全相等的;d=j-

HDU--1358--KMP算法失配函数getfail()的理解--Period

/* Name: hdu--1358--Period Author: 日天大帝 Date: 20/04/17 10:24 Description: 长度/向后移动的位数 = 出现的次数 kmp其实匹配到了第str.size()位,这一位原本是'\0'的, 但是由于里面的递推下一位的关系,这一位其实也是匹配了的: */ #include<iostream> #include<cstring> using namespace std; void getfail(string); int

POJ2406Power Strings

来源:http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30293   Accepted: 12631 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b =

POJ 2406Power Strings(KMP)

POJ 2406 其实就是一个简单的kmp应用: ans = n % (n - f[n]) == 0 ? n / (n - f[n]) : 1 其中f是失配函数 1 //#pragma comment(linker, "/STACK:1677721600") 2 #include <map> 3 #include <set> 4 #include <stack> 5 #include <queue> 6 #include <cmat