Period (next与循环串)
来源:互联网 发布:oracle删除重复数据 编辑:程序博客网 时间:2024/06/14 08:07
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 A K,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
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.
number zero on it.
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.
3aaa12aabaabaabaab0
Test case #12 23 3Test case #22 26 29 312 4
题目大意:给出一个长度为n的字符串,从长度为2的前缀开始,一一判断 前缀 是否为 循环串 构成,并输出 前缀长度 和 循环串的个数 。
e.g Test case #2中 aabaabaab 是长度为9的前缀,其循环串为aab,个数为3,故输出 9 3 。
知识点:如果i,满足i%(i-next[i])=0则长度为i的前缀,是由循环串构成, 其个数为i/(i-next[i])。
Tip:但如此会出现循环串个数为1的情况,与题意不符,故排除掉。
抄袭加原创代码(抄袭部分出处未知):
#include<stdio.h>int next[1000005];char a[1000005];void getnext(char *a,int n){int i,j;i=0,j=next[0]=-1;while(i<n){if(j==-1||a[i]==a[j]){i++,j++;next[i]=j;}else{j=next[j];}}}int main(){int n,i,t=1;while(~scanf("%d",&n)&&n!=0){scanf("%s",a);printf("Test case #%d\n",t++);getnext(a,n);for(i=2;i<=n;i++){if(i%(i-next[i])==0&&i/(i-next[i])>1){printf("%d %d\n",i,i/(i-next[i]));}}printf("\n");}}
阅读全文
0 0
- Period (next与循环串)
- HDU 1358 Period(next数组+周期+循环节)
- HDU 1358 Period (next跳表找最小循环节)KMP
- POJ 1961 Period(next数组求循环节)
- HDu 1358 Period(Next 数组找循环节)
- 【kmp算法next数组-串的最小循环节/循环周期】Period HDU
- hdu1358—Period(循环串)
- CUGB专题训练之数据结构:D - Period (kmp中的next判断循环节)
- LA 3026 && POJ 1961 Period (利用kmp中的next数组找最小的循环节 )
- Poj-1961 Period(next数组应用)
- HDOJ1358 Period (KMP next数组应用)
- HDU 1358 Period 【KMP】(子串循环次数)
- Period +uvalive+next数组
- Period next数组应用
- POJ 1961 Period KMP(next数组应用)
- POJ 1961 Period(KMP next数组巧用)
- HDOJ 1358 Period(KMP next数组运用)
- HDOJ 1358.Period(KMP中next数组应用)
- TensorFlow1.2的一个bug(Couldn't open CUDA library libcupti.so.8.0.)
- 守护(Daemon)线程
- Mybatis插件
- InitCommonControlsex
- 20个非常有用的java程序片段
- Period (next与循环串)
- FFmpeg之视频加水印
- Mybatis中Mapper内置方法细解
- 投影坐标,平面以及坐标转换
- 在 Laravel 5 中使用 Laravel Excel 实现 Excel/CSV 文件导入导出功能
- 自定义struts2拦截器
- 图像处理总结
- hdu 1173
- Cyclic Nacklace