【POJ 2406】 Strings

Strings

Time Limit: 3000MS Memory Limit: 65536K

Total Submissions: 35168 Accepted: 14543

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

kmp求循环节。

如上图所示,红色的串的尾部就是next[l],他和绿色的串分别代表字符串的前缀和后缀,他们是完全相同的(根据next[i])的定义。

那么蓝色和紫色部分(长度都是l?next[l])就是相同的。

因为他们分别是绿串和红串的尾部。

紫色和金色(等长)的部分也是相同的。

因为紫色部分是绿串从右往左数的第l?next[l]+1个,金色部分是红串从右往左数的第l?next[l]+1个。

两个三角部分是相同的。

因为他们分别是绿串和红串的前缀且长度相同。

接下来分情况讨论:

①三角部分的长度等于l?next[l]即l%(l?next[l])=0,说明该串的最短循环节长度为l?next[l],循环节个数为ll?next[l]

②三角部分的长度小于l?next[l]即l%(l?next[l])≠0,说明该串内部没有循环节,本身是循环节,循环节个数为1。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
char s[1000005];
int ne[1000005];
int main()
{
    while (scanf("%s",s+1))
    {
        if (s[1]==‘.‘) break;
        int j=0;
        int l=strlen(s+1);
        for (int i=2;i<=l;i++)
        {
            while (j&&s[i]!=s[j+1]) j=ne[j];
            if (s[i]==s[j+1]) j++;
            ne[i]=j;
        }
        if (l%(l-ne[l])==0) printf("%d\n",l/(l-ne[l]));
        else puts("1");
    }
    return 0;
}

附:kmp学习好文

时间: 2024-08-11 03:37:48

【POJ 2406】 Strings的相关文章

【Poj 2406】Power Strings

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 64676   Accepted: 26679 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 1408】 Fishnet (叉积求面积)

[POJ 1408] Fishnet (叉积求面积) 一个1*1㎡的池塘 有2*n条线代表渔网 问这些网中围出来的最大面积 一个有效面积是相邻两行和相邻两列中间夹的四边形 Input为n 后面跟着四行 每行n个浮点数 每一行分别代表a,b,c,d 如图 并且保证a(i) > a(i-1) b(i) > b(i-1) c(i) > c(i-1) d(i) > d(i-1) n(n <= 30)*2+4(四个岸)条边 枚举点数就行 相邻的四个四个点枚举 找出围出的最大面积 找点用

【POJ 2513】Colored Sticks

[POJ 2513]Colored Sticks 并查集+字典树+欧拉通路 第一次做这么混的题..太混了-- 不过题不算难 字典树用来查字符串对应图中的点 每个单词做一个点(包括重复单词 题意就是每个边走且直走一次(欧拉通路 欧拉图的判定: 没有或者只有两个奇数度的点的图叫做欧拉图 有这些就可以解答此题了 另外需要注意题目范围是25W个木棍 所以最多可能有50W个点 卡了好多个RE 代码如下: #include <iostream> #include <cstdlib> #incl

2292: 【POJ Challenge 】永远挑战

2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 553  Solved: 230[Submit][Status][Discuss] Description lqp18_31和1tthinking经常出题来虐ftiasch.有一天, lqp18_31搞了一个有向图,每条边的长度都是1. 他想让ftiasch求出点1到点 N 的最短路."水题啊.", ftiasch这么说道. 所以1tth

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

【POJ 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

【POJ 1228】Grandpa&#39;s Estate 凸包

找到凸包后暴力枚举边进行$check$,注意凸包是一条线(或者说两条线)的情况要输出$NO$ #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 1003 #define read(x) x = getint() using namespace std; inline int getint() { int k = 0, fh = 1; char c

【POJ 2750】 Potted Flower(线段树套dp)

[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4566   Accepted: 1739 Description The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrou