参考: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. [BZOJ1061][Noi2008]志愿者招募 线性规划+费用流

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1061 根据题意列方程,然后用网络流解线性规划. 题解直接贴ByVoid的吧,太神了:htt ...

  2. 触发器deleted 表和 inserted 表详解

    摘要:触发器语句中使用了两种特殊的表:deleted 表和 inserted 表. create trigger updateDeleteTimeon userfor updateasbegin  u ...

  3. HTTP/2 带来的新颖

    Binary Frames HTTP2 在TCP层之上 HTTP下增加了Binary Frames协议, 基于该协议HTTP Request/Response 可以被分拆成由Frame组成的Strea ...

  4. Jmeter之WebService接口测试

    一.简介  1.JMeter3.2前的版本,可以使用SOAP/XML-RPC Request插件直接进行webservice接口,而3.2后的版本则已经取消了这个接口,需要另外的方法才能进行测试. 2 ...

  5. sh InvocationTargetException

    话题引入: 使用hibernate+struts框架开发项目时,使用占位符时,系统抛出如下异常: Exception: java.lang.reflect.InvocationTargetExcept ...

  6. 背景渐变 background-image:linear-gradient(0deg,#fff,#ccc);

    背景渐变 background-image:linear-gradient(0deg,#fff,#ccc);

  7. CAD参数绘制实心圆弧填充(网页版)

    js中实现代码说明: function DrawPathToHatch1() { //把路径的开始位置移动指定的点 //参数一为点的X坐标 ,参数二为点的Y坐标,参数三为该点处开始宽度,对Polyli ...

  8. Matrix (二分套二分

    Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i ...

  9. 05EXtensible Stylesheet Language

    1. EXtensible Stylesheet Language 1. EXtensible Stylesheet Language XSL 指扩展样式表语言(EXtensible Styleshe ...

  10. 02CSS基本语法

    CSS基本语法 id选择符 在HTML文档中,需要唯一标识一个元素时,就会赋予它一个id标识,以便在对整个文档进行处理时能够很快地找到这个元素. 而id选择符就是用来对这个单一元素定义单独的样式.#号 ...