参考: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. 面试题6:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList

    题目 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 思路 使用栈依次存放输入的链表顺序的值,然后依次出栈便是链表的逆序. 代码 import java.util.ArrayList ...

  2. 【译】x86程序员手册36-9.9异常汇总

    9.9 Exception Summary 异常汇总 Table 9-6 summarizes the exceptions recognized by the 386. Table 9-6. Exc ...

  3. 5.4QBXT 模拟赛 (Rank1 机械键盘 蛤蛤)

    NOIP2016提高组模拟赛 ——By wangyurzee7 中文题目名称 纸牌 杯具 辣鸡 英文题目与子目录名 cards cups spicychicken 可执行文件名 cards cups ...

  4. OJB

    OJB 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 对象关系桥(OJB)是一种对象关系映射工具,它能够完成从Java对象到关系数据库的透明存储.   英文名 OJB ...

  5. day22-类的封装、property特性以及绑定方法与非绑定方法

    目录 类的封装 两个层面的封装 第一个层面 第二个层面 封装的好处 私有模块 类的propertry特性 setter 和 deleter 类与对象的绑定方法与非绑定方法 类的封装 将类的属性或方法隐 ...

  6. windows sdk编程禁止改变窗体大小

    #include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...

  7. java_StringBuffer、StringBuilder

    StringBuffer和StringBuider是可变的字符串,使用方法 相同,StringBuffer是线程安全的,StringBuider是线程不安全的 public class StringT ...

  8. 在mac下做web开发,shell常用的快捷键

    Ctrl + A 光标移动到行首 Ctrl + E 光标移动到行末 Ctrl + K 清屏(也可是用clear命令) Command +shift+{} 终端的tab左右切换

  9. VIM基础操作方法汇总

    学习自小甲鱼的视频,快速入门vim 目录: 1.光标移动 2.进入插入模式 3.进入普通模式 4.进入命令行模式 5.退出 6.光标跳跃 7.快速跳转行号 8.删除 9.利用数字重复操作 10.撤回 ...

  10. CSU 2018年12月月赛 F(2218): Finding prime numbers

    Description xrdog has a number set. There are 95 numbers in this set. They all have something in com ...