参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html

总结一下,如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且

循环节长度为:   i - next[i]

循环次数为:       i / ( i - next[i] )

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std; char a[];
int p[];
void next(int l)
{
int j=;
p[]=;
for(int i=;i<=l;i++)
{
while(j>&&(a[j+]!=a[i])) j=p[j];
if(a[j+]==a[i]) j+=;
p[i]=j;
}
}
int main()
{
//freopen("a.txt","r",stdin);
int n,j=;
while(~scanf("%d",&n))
{
if(n==) break;
printf("Test case #%d\n",j++);
scanf("%s",a+);
int l=strlen(a+);
next(l);
for(int i=;i<=l;i++)
{
if(i%(i-p[i])==&&p[i]!=)
{
printf("%d %d\n",i,i/(i-p[i]));
}
}
printf("\n");
}
return ;
}

Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)的更多相关文章

  1. UVALive - 3026 Period kmp next数组的应用

    input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...

  2. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

  3. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

  4. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  5. KMP求字符串最小循环节

    证明1: 对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即  i ...

  6. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

  7. 【暑假】[实用数据结构]UVAlive 3026 Period

    UVAlive 3026 Period 题目: Period   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

  8. poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 Descripti ...

  9. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

随机推荐

  1. hihocoder 神奇字符串

    思路: 暴力,模拟. 实现: #include <iostream> #include <algorithm> #include <cstdio> #include ...

  2. 修改vim注释字体颜色

    vim /etc/vimrc#最后一行新增代码块hi comment ctermfg=6 注:注释用 " 标注 PS:默认的注释颜色是4  然后有0,1,2,3,4,5,6,7来选择.可以除 ...

  3. Jquery+ashx实现Ajax

    一 Ajax的实现方式 1.使用一般的webform,在页面用jQuery ajax调用,再从取得的html数据中取得<body>内的内容,写入DOM 优点:不用改变现有的asp.net开 ...

  4. Python之三元运算、集合、函数

    一.三元运算符 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值 格式:[on_true] if [expression] else [on_false] res = 值1 if 条件 els ...

  5. python文件的读写的模式

    <1>打开文件 在python,使用open函数,可以打开一个已经存在的文件,或者创建一个新文件 open(文件名,访问模式) 示例如下: f = open('test.txt', 'w' ...

  6. Flask框架 之模版

    一.过滤器 safe:禁用转义: <p>{{ '<em>hello</em>' | safe }}</p> capitalize:把变量值的首字母转成大 ...

  7. python练习2 购物车程序

    # -*- coding: utf-8 -*-# @Time : 2018/10/18 16:06# @Author : Two Brother# @EMAIL : yeluyide@163.com# ...

  8. CAD参数绘制点(网页版)

    点在CAD中的作用除了可以分割对象外,还能测量对象,点不仅表示一个小的实体,而且通过点作为绘图的参考标记. pdmode是一个控制point的形式的系统变量,当pdmode=0时是可见的一个点,当pd ...

  9. 删数问题(Noip1994)

    1321:[例6.3]删数问题(Noip1994) 时间限制: 1000 ms         内存限制: 65536 KB提交数: 5127     通过数: 1595 [题目描述] 输入一个高精度 ...

  10. Linux安装64位Mysql5.7.22

    以安装在/usr/local目录下为例 1.下载安装包地址:https://dev.mysql.com/downloads/file/?id=476936,安装包保存到/usr/local 2.解压: ...