Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 14653   Accepted: 6965

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 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
题意:求前缀的长度,以及其中最小循环节的循环次数 但是循环次数必须大于1
#include<stdio.h>
#include<string.h>
#define MAX 1000100
int next[MAX];
char str[MAX];
int a[MAX],b[MAX];
int n,m,k;
void getfail()
{
int i,j;
next[0]=next[1]=0;
for(i=1;i<n;i++)
{
j=next[i];
while(j&&str[i]!=str[j])
j=next[j];
next[i+1]=str[i]==str[j]?j+1:0;
}
}
void kmp()
{
int i,j;
for(i=1;i<=n;i++)
{
k=i;
if(k==(k-next[k]))//k-next[k]最小循环节长度
continue;
if(k%(k-next[k])==0)
printf("%d %d\n",i,k/(k-next[k]));
}
printf("\n");
}
int main()
{
int i,t=1;
while(scanf("%d",&n),n)
{
getchar();
for(i=0;i<n;i++)
scanf("%c",&str[i]);
getfail();
printf("Test case #%d\n",t++);
kmp();
}
return 0;
}

  

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】的更多相关文章

  1. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  2. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

  3. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  4. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  5. HDU 3746 (KMP求最小循环节) Cyclic Nacklace

    题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...

  6. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

  7. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

  8. HDU 1358 Period 求前缀长度和出现次数(KMP的next数组的使用)

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

  9. poj 1961 Period 【KMP-next前缀数组的应用】

    题目地址:http://poj.org/problem?id=1961 Sample Input 3 aaa 12 aabaabaabaab 0 Sample Output Test case #1 ...

随机推荐

  1. 在2015中使用V12版本的ReportView控件,会导致winform窗体不能正常打开

    在2015中使用V12版本的ReportView控件,会导致winform窗体不能正常打开,使用V10版本没问题,但2015中默认使用的就是V12版本,所以需要避免使用V12版本

  2. php计算时间差/两个时间日期相隔的天数,时,分,秒.

    function timediff( $begin_time, $end_time ) { if ( $begin_time < $end_time ) { $starttime = $begi ...

  3. IOS开发备忘

    1. ios 真机调试时出现CopyPngFile error解决方法 说是读取的时候没有找到这张图片,检查了一下图片路径,没有问题,于是google之,找到两种解决方法 : 方法一:在build s ...

  4. 学会爱上iOS自动布局(Auto Layout) - 剑尖

    本文翻译自Yari Dareglia的LEARN TO LOVE AUTO LAYOUT文章先生们,女士们,让我们以正确的心态开始本教程吧:自动布局就是简单!我花了一段时间来掌握自动布局是如何工作的, ...

  5. 跨平台的zip文件压缩处理,支持压缩解压文件夹

    根据minizip改写的模块,需要zlib支持 输出的接口: #define RG_ZIP_FILE_REPLACE 0 #define RG_ZIP_FILE_APPEND 1 //压缩文件夹目录, ...

  6. 董的博客 hadoop

    董的博客 https://issues.apache.org/jira/browse/MAPREDUCE 很重要,把MAPREDUCE改为YARN即可 直接下载patch即可 http://horto ...

  7. Node.js流

    什么是流? 流是可以从一个源读取或写入数据到连续的目标对象.在Node.js,有四种类型的数据流. Readable - 其是用于读操作. Writable - 用在写操作. Duplex - 其可以 ...

  8. WIN版的Jenkins Master加入LINUX的SLAVE节点,并作C++程序的集成交付

    这次深撸了一下JENKINS的配置,不敢说完全通了. 但对于整个体系,有了更新认识. 将LINUX作为SLAVE节点加入WIN的JENKINS里,网上有很多教程,依作即可. 在将相关任务分配给这个节点 ...

  9. ibatis报错

    关键词:org.springframework.dao.DataIntegrityViolationException 在程序中进行数据库插入操作时报错如下: 未知异常:org.springframe ...

  10. FileUtils

    import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...