POJ 1961 kmp
来源:互联网 发布:营销网络地图制作 编辑:程序博客网 时间:2024/05/19 23:23
Period
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 12072 Accepted: 5645
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.
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
好久没见kmp了,水一道题回忆一下。
/* ***********************************************Author :rabbitCreated Time :2014/3/12 20:41:00File Name :1.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;char str[1001000];int next[1001000];int main(){ //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int n,ncase=0; while(~scanf("%d",&n)&&n){ scanf("%s",str); printf("Test case #%d\n",++ncase); int i,j; j=next[0]=-1; i=0; while(i<n){ while(j!=-1&&str[i]!=str[j])j=next[j]; next[++i]=++j; } for(int i=1;i<=n;i++){ j=i-next[i]; if(i!=j&&i%j==0)printf("%d %d\n",i,i/j); } puts(""); } return 0;}
0 0
- (KMP) poj POJ 1961 Period
- poj 1961(KMP)
- POJ 1961 KMP
- poj 1961:Period (KMP)
- 【KMP】POJ 1961
- POJ 1961 (KMP)
- POJ 1961 KMP
- poj 1961 Period (KMP)
- poj 1961 Period---kmp
- POJ 1961-Period:KMP
- poj 1961 Period KMP
- poj 1961 Period(KMP)
- POJ 1961 kmp
- Period - POJ 1961 KMP
- POJ-1961(KMP)
- POJ 1961 Period (KMP)
- poj--1961--KMP
- POJ Period 1961【KMP】
- Javascript中创建对象的几种方式
- Eclipse下安装Android开发环境:NDK和CDT
- (11)线程之join()
- Facebook的社交类Benchmark: LinkBench
- uva 609 - Metal Cutting(暴力)
- POJ 1961 kmp
- 递归实现数组中求最值和求和的算法
- Python初学之连接Oracle数据库
- 扑克牌花色
- [贪心]uva10020 Minimal coverage
- SIP协议初探
- 停车场
- 我的第一篇OPENGL
- glBlendFunc();--混合