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. 【热更新IK词典】ElasticSearch IK 自动热更新原理与实现

    一.热更新原理 elasticsearch开启加载外部词典功功能后,会每60s间隔进行刷新字典.具体原理代码如下所示: public void loadDic(HttpServletRequest r ...

  2. 【转载】Java关键字之"transient"

    原文出处:http://blog.csdn.net/lanxuezaipiao/article/details/16358677 transient的作用及使用方法 我们都知道一个对象只要实现了Ser ...

  3. 51Nod 1509 加长棒(隔板法)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1509 思路: 直接去解可行的方法有点麻烦,所以应该用总的方法去减去不可行 ...

  4. Flyweight(享元)

    意图: 运用共享技术有效地支持大量细粒度的对象. 适用性: 一个应用程序使用了大量的对象. 完全由于使用大量的对象,造成很大的存储开销. 对象的大多数状态都可变为外部状态. 如果删除对象的外部状态,那 ...

  5. angular项目一

    1.Angularjs第三方模块angular-route和angular-ui-router的区别.差异.不同, ui-router路由器是一个第三方模块,功能非常强大.它支持一切正常ngroute ...

  6. JS级联下拉框

    //Ajax级联获取SDKfunction GetDropDownList(parent_ddlID, fill_dllID, url, param) {    this.pId = parent_d ...

  7. centos7(debian,manjora,freebsd)命令及安装mysql、git、gpg、gogs,安装docker,zsh,chrome

    最小安装: 1. 选择English 2. DATE & TIME 修改好本地时间 SOFTWARE SELECTION默认的Minimal Install就好 INSTALLATION DE ...

  8. 数论练习(6)——hdu A/B(逆元gcd)

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. jackson springboot null节点忽略配置

    spring: jackson: date-format: yyyy-MM-dd HH:mm:ss default-property-inclusion: non_null spring.jackso ...

  10. MD5加密源码!

    import java.security.*; class MD5{ public final static String MD5(String s){ char hexDigits[] = {'0' ...