hdu 1358 period KMP
来源:互联网 发布:mysql @变量 编辑:程序博客网 时间:2024/06/08 10:41
Period
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
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 4
思路::若i%(i-next[i])=0,那么前i个字符为k=i/(i-next[i])个长度为i-next[i]的循环。 神奇的next数组。。。=.=
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int M = 1000010;int nxt[M];char T[M];int m;int ans;void getnext(){ int j,i, k; j = 0; k = -1; nxt[0] = -1; while(j<m){ if (k==-1 || T[j]==T[k]){ nxt[++j] = ++k; i=j; if(i%(i-nxt[i])==0&&nxt[i]) printf("%d %d\n",i,i/(i-nxt[i])); } else{ k = nxt[k]; } }}int main(){ // freopen("in.txt","r",stdin); int cs=0; while(scanf("%d",&m)!=EOF&&m){ cs++; scanf("%s",T); printf("Test case #%d\n",cs); getnext(); printf("\n"); } return 0;}
0 0
- HDU 1358 Period (KMP)
- HDU 1358 Period KMP
- hdu 1358 Period KMP
- hdu 1358 Period(KMP)
- HDU 1358 Period KMP
- 【HDU】1358 Period KMP
- HDU 1358 Period (KMP)
- hdu-1358 Period 【kmp】
- hdu 1358 Period ( kmp )
- HDU 1358--Period【KMP】
- HDU 1358 Period (KMP)
- hdu 1358 period KMP
- hdu 1358 Period(kmp)
- HDU-1358 Period (kmp)
- HDU 1358 Period kmp
- hdu 1358 Period (KMP)
- HDU 1358 Period [kmp]
- hdu 1358 Period(KMP循环)
- jquery获取class的值加入到数组,并判断数组中是否存在特定字符串
- iOS setter与getter方法
- XMLSchema 模式与命名空间
- 利用keepalived构建高可用MySQL-HA
- 通知中心与观察者模式
- hdu 1358 period KMP
- OC_AddressBook_通讯录
- Mac下打开多个Eclipse
- 计算机中的进制及编码
- 自适应高度
- Eclipse 在编辑窗口显示打印边距(换行参考线)
- iOS类别(Category)与扩展(Extension)
- Windows 服务的Session 0 隔离机制
- Android Universal Image Loader 源码分析(一)