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. burnside引理&polya定理

    burnside引理&polya定理 参考资料: <polya计数法的应用>--陈瑜希 黄学长 置换: 置换即是将n个元素的染色进行交换,产生一个新的染色方案. 群: 一个元素的集 ...

  2. 在触发器中,当“IsMouseOver”属性=true时,设置当前控件的高亮选中效果

    <Style.Triggers>            <!--<Trigger Property="IsSelected" Value="Tru ...

  3. PrestaShop 1.7 用户付款的时候无法支付错误

    用户付款的时候出现错误,错误的信息是没有支付方式. 这个有可能是你后台支付的国家限制没有选择. 请确定你已经选择了支付国家限制已经选择了. 当选择成功后,你应该可以看到下面的选择项.

  4. Informatica存储库和集成服务的配置

    1.配置存储库 首先登陆informatica的管理界面: 这里的用户名不区分大小写(注意后面配置集成服务是区分的,后面会提到)

  5. c# winform 操作oracle数据库的Blob字段,把图片存储到数据库,保存图片到数据库

    ///c# winform 操作oracle数据库的Blob字段,把图片存储到数据库,保存图片到数据库 闲话不多说,直接上代码 using System; using System.Collectio ...

  6. 用压测模拟并发、并发处理(synchronized,redis分布式锁)

    使用工具:Apache an 测压命令: ab -n 100 -c 100 http://www.baidu.com -n代表模拟100个请求,-c代表模拟100个并发,相当于100个人同时访问 ab ...

  7. 通过命令启动一个activity(am pm 命令)

    一.am的含义是activityManager 主要作用是启动activity.service .broadcast    1.通过adb命令启动acitvity,首先需要设置activity 的 e ...

  8. SpringMvc中的Interceptor拦截器的学习

    拦截器是SpringMvc框架中常用的一个东东,它跟Filter相似,但是也有区别,以前也没用过,今天看到就顺便学习了一下. SpirngMvc中的Interceptor主要是通过HandlerInt ...

  9. New Concept English Two 32 88

    $课文86  失控 940. As the man tried to swing the speedboat round, the steering wheel came away in his ha ...

  10. 在.NET开发中的单元测试工具之——NUnit

    NUnit介绍 NUnit是一个专门针对于.NET来写的单元测试框架,它是xUnit体系中的一员,在xUnit体系中还有针对Java的JUnit和针对C++的CPPUnit,在开始的时候NUnit和x ...