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. Redis之List 列表

    Redis List 列表 Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (42949672 ...

  2. UVa 10491 奶牛和轿车(全概率公式)

    https://vjudge.net/problem/UVA-10491 题意: 假设有a头牛,b辆车,在最终选择前主持人会替你打开c个有牛的门,输出"总是换门"的策略下,赢得车的 ...

  3. Java中含有静态成员的的初始化顺序

    class Bowl{ Bowl(int marker){ System.out.println("Bowl(" + marker + ")" ); } voi ...

  4. 指定html转pdf文档

    1.资源 <script type="text/javascript" src="./js/canvg2.js"></script> & ...

  5. 使用H5 canvas画一个坦克

      具体步骤如下:   1. 首先做出绘图区,作为坦克的战场   <canvas id="floor" width="800px" height=&quo ...

  6. Spring中的@Transactional

    spring中的@Transactional基于动态代理的机制,提供了一种透明的事务管理机制,方便快捷解决在开发中碰到的问题. 一般使用是通过如下代码对方法或接口或类注释: @Transactiona ...

  7. Java网络编程和NIO详解6:Linux epoll实现原理详解

    Java网络编程和NIO详解6:Linux epoll实现原理详解 本系列文章首发于我的个人博客:https://h2pl.github.io/ 欢迎阅览我的CSDN专栏:Java网络编程和NIO h ...

  8. xtrabackup三种备份和还原(一)

    写这边博客心情不是太美好(博客已经停更2个多月了,实在是没心情学习新东西.2018我的黑暗年,呵呵)好了,不废话了,本文没有任何原理的部分,我也是刚开始接触xtrabackup这个工具.本文应该是一个 ...

  9. Qt UI界面改了,但UI界面不更新

    /**************************************************************************** * Qt UI界面改了,但UI界面不更新 * ...

  10. HDU3335 Divisibility Dilworth定理+最小路径覆盖

    首先需要一些概念: 有向图,最小路径覆盖,最大独立集,Dilworth,偏序集,跳舞链(DLX).... 理解一: 对于DAG图,有:最大独立集=点-二分匹配数,二分匹配数=最小路径覆盖. 而无向图, ...