Period

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 53   Accepted Submission(s) : 27

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

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 AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input file 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 3 Test case #2
2 2
6 2
9 3
12 4
题目大意:
例如aabaabaabaab 在长度为2时,aa是连续两个a相等,则得到结果是2,aabaab是连续两个aab相等,得到结果是2;以此类推就知道意思了。不能是ababa的aba aba连续两个相等,是分开的,和poj2406那题很像 。
解题思路:
用公式(l % (l - next[l]) == 0) 来判断是否子串重复,还要判断是否( l / (l -  next[l]) >1) ;
 
 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int n;
char a[];
int nxt[];
void get_nxt()
{
nxt[] = -;
int i = , j = -;
while (i < n)
{
if (j == - || a[i] ==a[j])
{
nxt[++i] = ++j;
}
else
{
j = nxt[j];
}
}
}
int main()
{
int k = ;
int i;
while (cin >> n && n)
{
cin >> a;
get_nxt();
/*for (i = 0; i <= n; i++) cout << nxt[i];*/
printf("Test case #%d\n", k++);
for (int i = ; i <= n; i++)
{
int cnt = i - nxt[i];
if (i % cnt == && i / cnt>)//多次循环,且不是它本身
{
printf("%d %d\n", i, i / cnt);
}
}
printf("\n");
}
}

HDU 1358 Period (kmp判断循环子串)的更多相关文章

  1. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  2. HDU 1358 Period(KMP计算周期)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

  3. HDU 1358 Period KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...

  4. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  6. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  7. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  8. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

  9. hdu 1358 Period (KMP求循环次数)

    Problem - 1358 KMP求循环节次数.题意是,给出一个长度为n的字符串,要求求出循环节数大于1的所有前缀.可以直接用KMP的方法判断是否有完整的k个循环节,同时计算出当前前缀的循环节的个数 ...

随机推荐

  1. UVa 11248 网络扩容(最大流(需要优化))

    https://vjudge.net/problem/UVA-11248 题意: 给定一个有向网络,每条边均有一个容量.问是否存在一个从点1到点N,流量为C的流.如果不存在,是否可以恰好修改一条弧的容 ...

  2. BZOJ 3676 【APIO2014】 回文串

    题目链接:回文串 我终于也会回文自动机辣! 其实吗……我觉得回文自动机(听说这玩意儿叫\(PAM\))还是比较\(simple\)的……至少比\(SAM\)友善多了…… 所谓回文自动机,每个节点就代表 ...

  3. mybatis generator为实体类生成自定义注释(读取数据库字段的注释添加到实体类,不修改源码)

    我们都知道mybatis generator自动生成的注释没什么实际作用,而且还增加了代码量.如果能将注释从数据库中捞取到,不仅能很大程度上增加代码的可读性,而且减少了后期手动加注释的工作量. 1.首 ...

  4. Android 3.0 r1 API中文文档(108) —— ExpandableListAdapter

    前言 本章内容是android.widget.ExpandableListAdapter,版本为Android 3.0  r1,翻译来自"深夜未眠",欢迎访问它的博客:" ...

  5. [Checking for libstdc++-4.4.4-13.el6-i686; Not found. Failed] 的解决。

    单纯 yum install libstdc++-4.4.4.i686 是不行的. 应该安装 yum install libstdc++-devel.i686 顺带就能装上需要的lib 真够变态的. ...

  6. 关于UDP很好的书籍和文章(整理、持续更新)

    文章 告知你不为人知的 UDP:疑难杂症和使用(必看)

  7. Rails 5 Test Prescriptions 最后一章,如何测试继承下来的代码,legacy code

    Set expectations 你不可能把一个老旧的代码野兽只用一晚就转变成优雅的奇迹marvel.你需要如下做法: 让自己有好的状态,用15分钟挥舞拳头诅咒之前的程序员 开始工作,这个codeba ...

  8. find ... -exec ... {} \; 的解释

    find的特殊功能是能够进行额外的动作,如上图的 find / -type f -name "test.txt" -exec rm {} \;命令 1) {} 代表的是由find找 ...

  9. torchnet package (1)

    torchnet package (1) torchnet torchnet torchnet是用于torch的代码复用和模块化编程的框架,主要包含四个类 Dataset 以不同的方式对数据进行预处理 ...

  10. windowplayer播放列表属性

    ArrayList a = new ArrayList(); a.Add("c:\\kugou\\林宇中 - 干物女.mp3"); a.Add("c:\\kugou\\海 ...