题干:

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 prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the 
number zero on it.

Output

For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3
aaa
12
aabaabaabaab
0

Sample Output

Test case #1
2 2
3 3Test case #2
2 2
6 2
9 3
12 4

题目大意:

给你一个字符串,求这个字符串到第i个字符为止的子字符串的循环节的次数,输出i和循环次数。

解题报告:

运用next数组可以直接出结果、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e6 + 5;
char s[MAX];
int Next[MAX],len;
void getnext() {Next[0] = -1;int k = -1,j = 0;int len = strlen(s);while(j < len) {if(k == -1 || s[k] == s[j]) {k++,j++;Next[j] = k;} else k = Next[k];}
}
int main() {int n,iCase = 0;while(~scanf("%d",&n)) {if(n == 0) break;scanf("%s",s);getnext();printf("Test case #%d\n",++iCase);for(int i=2; i<=n; i++) {int tmplen=i-Next[i]; //循环节长度if(Next[i]>0 && i % tmplen == 0) printf("%d %d\n",i,i/tmplen);}printf("\n");}return 0;
}

【POJ - 1961】Period(KMP,循环节问题)相关推荐

  1. poj 1961 Period(KMP)

    题目链接:http://poj.org/problem?id=1961 题目大意:给出一个长为n的字符串,求到每个字符之前有多少个字串循环次数大于1 方法: kmp ,求出这个字符串的next数组.在 ...

  2. poj 1961 Period

    这道题跟上一道题目差不多.一开始WA了一次.我以为他给的字符串本身就是周期串,然后写完交了就WA,崔老师又说这个串不一定是周期串然后果断改了上一题的笨法子,然后WA,然后又改成现在的算法= =...然 ...

  3. POJ - 1961 Period(nex数组的运用)

    https://vjudge.net/contest/189927#problem/E 题意: 给定一个字符串,问这个字符串的所有前缀中,有哪些前缀可以由某个串重复k次组成,并且这个k最大是多少.这个 ...

  4. Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)

    参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html 总结一下,如果对于next数组中的 i, 符合 i % ( i - n ...

  5. 嫦娥奔月(KMP,找循环节)及其扩展KMP

    问题描述 <归妹>卦辞为:昔者恒我(姮娥)窃毋死之药于西王母,服之以(奔)月.将往,而枚占于有黄.有黄占之曰:"吉.翩翩归妹,独将西行.逢天晦芒,毋惊毋恐,后且大昌". ...

  6. 【HDU - 3746 】Cyclic Nacklace (KMP,最小循环节问题)

    题干: CC always becomes very depressed at the end of this month, he has checked his credit card yester ...

  7. 【文文殿下】浅谈KMP算法next数组与循环节的关系

    KMP算法 KMP算法是一种字符串匹配算法,他可以在O(n+m)的时间内求出一个模式串在另一个模式串下出现的次数. KMP算法是利用next数组进行自匹配,然后来进行匹配的. Next数组 Next数 ...

  8. MUV LUV EXTRA -( kmp最小循环节 )

    题目链接:点击进入 题意 一个无限循环小数( 从小数点后 某一位 开始依次不断地重复出现前一个或一节数字的十进制无限小数,叫做循环小数 ),真实价值 = a * p - b * l ( a , b 是 ...

  9. hdu 3746 kmp求循环节 下标从1开始

    长度为m[1,2...m]的模式的循环节为 m-next[m] , aaa  循环节clc为1  (clc=m-next[m]= 3-2  =1)       此时   m%clc == 0   表示 ...

最新文章

  1. 取得Repeater内部控件命令名与命令参数
  2. mysql+dump+选项_详解MySQL数据备份之mysqldump使用方法
  3. hp打印机怎么连接电脑_hp打印机驱动怎么共享 惠普打印机如何安装【详解】
  4. nyoj 211 (Floyd算法求传递闭包)
  5. 干货 | BBR及其在实时音视频领域的应用
  6. python获取当前目录_又有几个Python小技巧分享
  7. Linux内核分析 - 网络[九]:邻居表
  8. java总结第四次//常用类
  9. 20190226work
  10. 不可磨灭的记忆 CPU发展史经典回顾
  11. ASP.NET程序中常用代码汇总-1
  12. 程序员必备简捷开发辅助工具总结
  13. EasyUI:datagrid分页显示+嵌入select下拉选项框
  14. matlab三次样条拟合,【MATLAB编程】三次样条
  15. 仿Google+相册的动画
  16. 【HUST】网安|编译原理实验|实验四攻略
  17. 监控摄像机选型攻略之常见机型选用
  18. 【PyQt5】教你一招,用timer计时器做个打地鼠的小游戏
  19. 电容屏物体识别_一种基于触摸屏触摸点的物体识别方法与流程
  20. PHP 获取客户端 IP 地址

热门文章

  1. 第七章 假设检验(2)
  2. [运动][组合]睡前运动
  3. 分页的limit_20.MySQL中的LIMIT(分页)
  4. node 更新_被创造者嫌弃,Node.js 如何应对来自 Deno 的挑战
  5. 2017安徽二级c语言,2017计算机二级C语言测试题及答案
  6. c语言编程流水灯与交通灯实验,C51单片机实验报告_流水灯_交通灯_定时器_双机交互_时钟.doc...
  7. java 正则表达式 替换字符串img标签的路径_正则表达式和有限自动机的应用
  8. UE4 查看打包文件内容
  9. Sqlite SQL格式化输入函数splite3_mprintf
  10. 如何构建自己的SIP SERVER!